2

I m new to Zend framework and using framework 2.0. I need certain clarification on routing in PHP Zend framework. 1) What is the role of Bootstrap.php in routing. 2) How we can user index.php for routing. 3) What code needs to be written to achieve custom routing.

Also, i have written certain code, but not getting desired output.

Bootstrap.php:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initRouter() { $frontController = Zend_Controller_Front::getInstance(); $router = $frontController->getRouter(); $route = new Zend_Controller_Router_Route(':userid', array('controller' => 'user', 'action'=> 'index', 'userid'=>null)); $router->addRoute('default', $route); } } 

index.php

 error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('display_errors', true); //Define Base Url define("BASEURL","http://".$_SERVER['SERVER_NAME']."/sampleapp/public/"); // directory setup and class loading set_include_path('.' . PATH_SEPARATOR . 'library/' . PATH_SEPARATOR . 'application/models' . PATH_SEPARATOR . 'application/config' . PATH_SEPARATOR . get_include_path() ); include ('Zend/Loader/Autoloader.php'); $loader = Zend_Loader_Autoloader::getInstance(); // setup controller $frontController = Zend_Controller_Front::getInstance(); $frontController->throwExceptions(true); $frontController->setControllerDirectory('application/controllers'); // run! $frontController->dispatch(); 

I'm trying to get route values, like my url is:

localhost:8080/sampleapp/user/index/1. 

How should i get 1 from route. here 1 is userid.

1
  • 1
    All code you have provided is Zend-Framework 1, therefore i will retag this for better help for you. Commented Dec 5, 2012 at 11:05

1 Answer 1

2

I would recommend to do the following:

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

    $router->addRoute('sample-module/user', new Zend_Controller_Router_Route( 'user/:action/:id/', array('module' => 'sample', 'controller' => 'user', 'id'=>'\w+') )); 

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

These are some useful links:

Zend routing, throws resource not found

http://framework.zend.com/manual/1.12/en/zend.controller.router.html#zend.controller.router.routes.standard

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.