【发布时间】:2012-12-03 10:43:32
【问题描述】:
这里,在文档中,是关于如何创建路由的: http://docs.phalconphp.com/en/latest/reference/routing.html
但我找不到如何将它们注入应用程序。 我需要做什么,让我的应用程序使用定义的路由?
我应该注入路由器(或如何注入?)
【问题讨论】:
这里,在文档中,是关于如何创建路由的: http://docs.phalconphp.com/en/latest/reference/routing.html
但我找不到如何将它们注入应用程序。 我需要做什么,让我的应用程序使用定义的路由?
我应该注入路由器(或如何注入?)
【问题讨论】:
可以通过这种方式在 DI(在您的 public/index.php 中)注册路由器:
$di->set('router', function() {
$router = new \Phalcon\Mvc\Router();
$router->add("/login", array(
'controller' => 'login',
'action' => 'index',
));
$router->add("/products/:action", array(
'controller' => 'products',
'action' => 1,
));
return $router;
});
也可以通过这种方式将路由注册移动到应用程序中的单独文件(即 app/config/routes.php):
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
然后在app/config/routes.php文件中:
<?php
$router = new \Phalcon\Mvc\Router();
$router->add("/login", array(
'controller' => 'login',
'action' => 'index',
));
$router->add("/products/:action", array(
'controller' => 'products',
'action' => 1,
));
return $router;
示例:https://github.com/phalcon/php-site/blob/master/public/index.php#L33 和 https://github.com/phalcon/php-site/blob/master/app/config/routes.php
【讨论】: