我开发了一个具有以下目录结构的phalcon Web应用程序:
/app/
/cache/
...
/config/
config.php
loader.php
services.php
/controllers/
contorllerBase.php
...
/models/
...
/views/
...
/public/
/css/
/img/
/js/
.htacces
index.php
webtools.config.php
webtools.php
index.html
我用phalcon devtools创建了这个项目,到目前为止它的工作正常,但现在我必须为这个项目实现REST功能。 我的问题是:
我在互联网上找到了一些解决方案,但它们让我很困惑,因为大多数情况下实现一个没有视图的REST api。
的config.php
return new \Phalcon\Config(array(
'database' => array(
...
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'cacheDir' => __DIR__ . '/../../app/cache/',
'logsDir' => __DIR__ . '/../../app/logs',
'baseUri' => '/',
)
));
loader.php
<?php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir
)
)->register();
services.php
<?php
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
/**
* Setting up the view component
*/
$di->set('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
}, true);
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use ($config) {
return new DbAdapter(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
"charset" => $config->database->charset
));
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
的index.php
<?php
error_reporting(E_ALL);
$debug = new \Phalcon\Debug();
$debug->listen();
try {
/**
* Read the configuration
*/
$config = include __DIR__ . "/../app/config/config.php";
/**
* Read auto-loader
*/
include __DIR__ . "/../app/config/loader.php";
/**
* Read services
*/
include __DIR__ . "/../app/config/services.php";
/**
* Handle the request
*/
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch (\Exception $e) {
echo $e->getMessage();
}
我真的很赞赏任何帮助。
答案 0 :(得分:4)
我用过:
<强> ROUTING 强>:
在路由文件中(非API部分的路由)
include __DIR__ . '/routes_api.php';
$router->mount($api);
在此routes_api中,我创建了一个组,并为该组定义了控制器的命名空间
$api = new \Phalcon\Mvc\Router\Group(array(
'namespace' => '\X\Controllers\API',
));
// --- Base API URL prefix
$api->setPrefix('/api');
路由按照惯例定义,使用REST服务,例如:
$api->addGet('/addresses', array('controller' => 'addresses', 'action' => 'listMe'));
<强>控制器:强>
我在控制器下创建了一个文件夹 api ,类在命名空间中作为组(名称空间X \ Controllers \ API)中定义的类,其中一个类库实现了一些REST礼貌方法所有REST控制器:
class AddressesController extends \X\ApiControllerBase
控制器基础:
class ApiControllerBase extends \Phalcon\Mvc\Controller
为所有REST提供JSON样式响应的自定义实现
public function initialize()
{
$this->response = new \X\ApiResponse();
}
通过OAuth服务器为REST部分提供授权网关,并对入站查询字符串进行一些获取/过滤/清理以获取分页和其他可用于我已覆盖的所有REST控制器的实用程序:
public function beforeExecuteRoute($dispatcher)
在控制器操作中,我使用一种方法返回响应,该方法从我的自定义Response实现(后面跟随)中的paginator获取数据:
$this->response->setResponse($paginator);
return $this->response;
对于输出样式:
class ApiResponse extends \Phalcon\Http\Response
{
public function __construct($content=null, $code=null, $status=null)
{
parent::__construct($content, $code, $status);
$this->setHeader('Content-Type', 'application/json');
}
public function setResponse($response, $limit = null, $processitems = null)
{
// .... some manipulations of data from controllers ... building of arrays ....
$this->setContent(json_encode(array('error' => false) + $response));
}
public function setResponseError($description, $error = true) {
//Set status code
$this->setStatusCode(200, 'OK');
$this->setContent(json_encode(array('error' => $error, 'error_description' => $description)));
}
}
需要一个控制器来管理对 / api 的请求而不做任何休息操作,默认情况下它应该以路由命名,所以它应该是ApiController,你可以调整路由系统以便更改或引发错误(http://docs.phalconphp.com/en/latest/reference/dispatching.html#inject-model-instances)
<强>服务:强>
最后,在index.php中管理几种错误(抛出异常),输出结果为JSON {error:true,message:&#34; ....&#34;}我已经实现了调度程序的 beforeException 事件:
$di->setShared('dispatcher', function() {
$eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach("dispatch", function($event, $dispatcher, $exception) {
/* @var $dispatcher Phalcon\Mvc\Dispatcher */
if ($event->getType() == 'beforeException') {
$ctrl = $dispatcher->getActiveController();
if($ctrl instanceof \X\ApiControllerBase) {
$dispatcher->forward(array(
'namespace' => '\\',
'controller' => 'error',
'action' => 'api',
'params' => array('message' => $exception->getMessage())
));
return false;
}
$dispatcher = new Phalcon\Mvc\Dispatcher();
//Bind the EventsManager to the Dispatcher
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
);
然后是调度程序使用forward方法调用的错误控制器(在这种情况下没有命名空间,它在控制器文件夹中):
class ErrorController extends \Phalcon\Mvc\Controller {
public function route404Action() {
$this->response->setStatusCode(404 , 'Not Found');
}
public function apiAction() {
$pars = $this->dispatcher->getParams();
$this->response = new \X\ApiResponse();
$this->response->setResponseError($pars['message']);
return $this->response;
}
}
希望有所帮助:)