Simple rewrites in Zend Framework

前端 未结 2 576
梦毁少年i
梦毁少年i 2020-12-11 22:50

This seems like a very simple question, but I\'ve only found complicated answers. I\'ve got a Zend Framework application that requires users to login. The loginAction(

相关标签:
2条回答
  • 2020-12-11 23:29

    for a defined purpose like you have a "named" route would be the simplest way to do it. While there are any number of ways to implement a named route the easiest is to put it in the application.ini:

        // /application/configs/application.ini
        resources.router.routes.login.route = /login
        resources.router.routes.login.defaults.module = default
        resources.router.routes.login.defaults.controller = auth
        resources.router.routes.login.defaults.action = login
    

    putting it in your bootstrap is not wrong, it just doesn't seem as convienient to me.
    Also doing it this way should (no guarantees) prevent any problems with the default routes.

    When calling a route using the url() helper it is important to remember to use either the named route :

    <?php echo $this->url(array(), 'routeName') ?>
    

    or if you need to pass the normal 'controller' => , 'action' => :

    <?php echo $this->url(array('controller' => 'index', 'action' => 'index'), 'default') ?>
    

    near as I can tell 'default' in this context indicates this would be a default route as defined in Zend/Controller/Router/Route/Module.php

    0 讨论(0)
  • 2020-12-11 23:29

    Just in case that you are interested in how to do it in your bootstrab in correct way.

    If you have only one route to rewrite, just add function:

    protected function _initRoute() {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $router->addRoute('login', //key of route !
            new Zend_Controller_Router_Route(
                'login', //this is url(www.url.com/login) that you want to rewrite, you can set whatever you want
                array(
                    'module' => 'default',
                    'controller' => 'auth',
                    'action' => 'login',
                )
        ));
    }
    

    Now, every www.your-url.com/login request will point to www.your-url.com/default/auth/login (module/controller/action)

    Edit:


    If you want to use your new url in view file, you have to use key for that route

    in view.phtml

     <a href="<?php echo $this->url(array(), 'login'); ?>">
       [link]
     </a>
    

    The first parameter is array(), and the second one is key which you define in bootstrap. Than if you change URL to /new-login in bootstrap, all URLs will be changed in view files.

    0 讨论(0)
提交回复
热议问题