如何在phalcon框架中应用路由器/ roite?

ava*_*sin 0 php routing phalcon

这里,在docs中,写了关于如何创建路由的文章:http: //docs.phalconphp.com/en/latest/reference/routing.html

但我无法找到我如何在应用程序中注入它们.我需要做什么,让我的应用程序使用定义的路由?

我应该注入路由器(或如何?)

twi*_*tra 20

路由器可以通过这种方式在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;
});
Run Code Online (Sandbox Code Playgroud)

也可以通过这种方式将路由注册移动到应用程序中的单独文件(即app/config/routes.php):

$di->set('router', function(){
    require __DIR__.'/../app/config/routes.php';
    return $router;
});
Run Code Online (Sandbox Code Playgroud)

然后在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;
Run Code Online (Sandbox Code Playgroud)

示例:https://github.com/phalcon/php-site/blob/master/public/index.php#L33https://github.com/phalcon/php-site/blob/master/app/config/ routes.php文件

  • Phalcon网站上的文档还有很多不足之处.谢谢! (3认同)