Search code examples
phpzend-frameworkhrefroutes

Simple route with Zend framework 1.12


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


Solution

  • 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: