Simple route with Zend framework 1.12

痞子三分冷 提交于 2020-01-01 15:37:15

问题


I'm looking for something that is simple but I don't know how to do it after many search. I look at the documentation of Zend 1.12 Route but I don't really understand.

I have these page in the Zend Framework : application/views/scripts/index/ index.phtml contact.phtml

In the application/views/layouts/scripts/layout.phtml

I want to href to contac.phtml for example. I'm looking for something to do like :

$this->url('contact')

Then, it redirect to the page contact... But I tried to add an route in the bootstrap.php but I don't really know how...

$router->addRoute('contact',
              new Zend_Controller_Router_Route('application/scripts/index/contact.phtml'));

Thank you,

David


回答1:


I think this is the simple codes for routing in zend framework:

  • on index.php you should not touch anything. Leave it as it is from Zend default when you create a project

  • on projectHomeDirectory/application/Bootstrap.php include this:

    protected function _initRoutes()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        include APPLICATION_PATH . "/configs/routes.php";
    }
    
  • create a routes.php file under projectHomeDirectory/application/configs/ and add there all the routes you want, for example:

    $route = new Zend_Controller_Router_Route(
        'author',
        array(
            'controller' => 'user',
            'action'     => 'index'
        ) 
    );
    
    $router->addRoute('author', $route);
    

Of course you then need to create the UserController the User model the sample module and the views.

Useful link:

  • http://framework.zend.com/manual/1.12/en/zend.controller.router.html



回答2:


I use the router resource plugin, I find it convenient to add the routes to my config files (for example the application.ini).

You can find an example and more documentation on the ZF website:

http://framework.zend.com/manual/1.12/en/zend.application.available-resources.html#zend.application.available-resources.router

Good luck building your ZF application!



来源:https://stackoverflow.com/questions/19556182/simple-route-with-zend-framework-1-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!