SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zend Custom Routing

    Trying to understand Custom routes. I basically have no problem making route settings in application.ini but I want to do it another way. I checked the documentation but it aint clear as to where im supposed to place this code, for example:

    PHP Code:
    <?php

    $router 
    = new Zend_Controller_Router_Rewrite();
    $route = new Zend_Controller_Router_Route(
    'product/:name',
    array(
    'controller' => 'products',
    'action' => 'display'
    )
    );

    $router->addRoute('product',$route);
    ?>
    So do I place it in boostrap.php, index.php or the controller in this case "products"? Else I'm I missing something?

    project/directory structure: /Applications/MAMP/htdocs/zfproject/

    Code:
    zfproject
      - application
        - configs
          application.ini
        + controllers
        + layouts
        + modules
        + views
          bootstrap.php
      + docs
      - library
        + Zend
      - public
          .htaccess
          index.php
      + tests
    Thanks

  2. #2
    SitePoint Member
    Join Date
    Mar 2012
    Location
    Raleigh-Durham, NC
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Routes should go in your bootstrap file. They affect the entire application and are processed before the code even gets to the controller, so putting them in the controller would not work. Technically, you could also put routing info in index.php, I worked fo a company that did that, but it's messy and less organized than putting it in the bootstrap file.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Uk
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I have them set in my bootstrap.php and Im trying to access the variable but i am not getting anything: (Using version 1.11)

    PHP Code:
    //BootStrap.php

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {

    public function 
    initWithRoutes(){
            
    $frontController Zend_Controller_Front::getInstance();
            
    $router $frontController->getRouter();

            
    $router->addRouter(
                    
    'itemParam',
                    new 
    Zend_Controller_Router_Route(
                            
    '/display/:item',
                            array(
    'controller' => 'products''action' => 'display')
                    )
            );
    }


    And this is my Controller:


    PHP Code:
    //ProductsController.php

    class ProductsController extends Zend_Controller_Action
    {

        public function 
    init()
        {
            
    /* Initialize action controller here */
        
    }

        public function 
    indexAction()
        {
            
    // action body
        
    }
        public function 
    displayAction(){
            if(
    $this->getRequest()->getParam('item')){
                
    $this->view->product $this->_request()->getParam('item');
                
            } else {
                
    $this->view->product 'nothing was passed';
            }
        }

    And finally my view:

    PHP Code:

    //display.phtml

    <?php echo $this->product?>
    I wonder what I'm now getting wrong


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •