【问题标题】:How router/roites could be injected in application in phalcon framework?如何将路由器/路由注入到 phalcon 框架的应用程序中?
【发布时间】:2012-12-03 10:43:32
【问题描述】:

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

但我找不到如何将它们注入应用程序。 我需要做什么,让我的应用程序使用定义的路由?

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

【问题讨论】:

    标签: php routing phalcon


    【解决方案1】:

    可以通过这种方式在 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#L33https://github.com/phalcon/php-site/blob/master/app/config/routes.php

    【讨论】:

    • Phalcon 网站上的文档还有很多不足之处。谢谢!
    • 确实,这更加具体,并且可以轻松添加自定义路线
    • 感谢您的详细解答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2019-03-23
    • 1970-01-01
    • 2016-09-10
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多