[英]Phalcon PHP: is there a way to do automatic resource routing sans annotations?
我想使用Phalcon PHP进行资源路由-但我不想指定所有这些注释就可以做到这一点。 有没有办法设置它,使其更加自动化? 您知道吗,例如为特定的路由类型定义默认操作?
如:
GET / resources => listAction
POST /资源=> createAction
等等
您可以通过多种方式配置路由, 文档中对此进行了广泛介绍 。 您可以像下面那样设置通用路由,这通常是在配置中完成的。 这也是默认路由,无需配置即可工作(可能除了方法外)。
// Create the router
$router = new \Phalcon\Mvc\Router();
//Define a route
$router->add(
"/:controller/:action/:params",
array(
"controller" => 1,
"action" => 2,
"params" => 3,
)
)->via(array("POST", "GET"));
您还可以扩展默认路由器以覆盖handle
方法,并在那里指定所有逻辑。 在这两种情况下,必须将已配置的路由器注入DI中。
您可以指定http-method-types应该调用哪个控制器/动作,如文档所述。
// This route only will be matched if the HTTP method is GET
$router->addGet("/products/edit/{id}", "Products::edit");
// This route only will be matched if the HTTP method is POST
$router->addPost("/products/save", "Products::save");
// This route will be matched if the HTTP method is POST or PUT
$router->add("/products/update")->via(array("POST", "PUT"));
除此之外,还有一种方法可以通过Phalcon \\ Events \\ Manager处理调度程序服务中无法识别的控制器和动作,如下所示。
// config/services.php
use Phalcon\Events\Manager as EventsManager;
//...
/**
* Dispatcher use a default namespace
*/
$di->set("dispatcher", function () {
// catch dispatcher exceptions for HANDLER_NOT_FOUND and ACTION_NOT_FOUND
// @see http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Dispatcher.html
// @see http://forum.phalconphp.com/discussion/525/404-and-notfoundaction#C2179
$evManager = new EventsManager();
$evManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(array(
'controller' => 'index',
'action' => 'show404',
));
return false;
}
});
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("YourCustomNamespace\\Controllers");
$dispatcher->setEventsManager($evManager);
return $dispatcher;
});
这意味着您可以使用Phalcon \\ Mvc \\ Router的默认行为,定义自定义路由并为无法由Phalcon \\ Mvc \\ Router :: notFound()无法处理的默认行为处理的路由做好准备。
编辑/其他信息:您可以使用php $this->request->isPut()//isDelete and so on...
在您的控制器内部找出发送的请求类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.