Results 1 to 4 of 4

Thread: Forcing ZF to use index controller and index action always

  1. #1
    flighty is offline Junior Member
    Join Date
    Aug 2009
    Posts
    4

    Default Forcing ZF to use index controller and index action always

    Hi

    I have a simple PHP app built using ZF and I want to force it to use the index controller and the index action all the time, irrespective of the URL. e.g.:

    http://mydomain/ -> this should route to index controller and index action

    http://mydomain/test -> this should route to index controller and index action but should pass "test" as a variable

    http://mydomain/test/test2 -> this should route to index controller and index action but should pass "test" and "test2" as variables.

    I have this code in my bootstrap file but it doesn't seem to achieve anything, as requests to http://mydomain/test cause an exception saying "Invalid controller specified (test)"

    Code:
    //Setup front controller
    $frontController = Zend_Controller_Front::getInstance();
    
    $route = new Zend_Controller_Router_Route(
                 ':controller/:action/*'
                 ,array('controlller' => 'index'
                 ,'action' => 'index'
                 ));
    
    $router = $frontController->getRouter();
    $router->addRoute('default', $route);
    $frontController->setRouter($router);
    Anyone got any ideas how I might achieve this?

  2. #2
    alokin is offline Senior Member
    Join Date
    Apr 2009
    Posts
    211

    Default

    I usually put routes configuration in some .ini file, and in that case, your route config should look something like this:
    Code:
    routes.some_route.type = "Zend_Controller_Router_Route"
    routes.some_route.route = ":test/:test2"
    routes.some_route.defaults.module = default
    routes.some_route.defaults.controller = index
    routes.some_route.defaults.action = index
    routes.some_routes.defaults.test = false
    routes.some_routes.defaults.test2 = false
    And then in your bootstrap:
    Code:
    $routesConfig = new Zend_Config_Ini('path/to/routes_config.ini');
    		
    $front = Zend_Controller_Front::getInstance();
    $front->getRouter()->addConfig($routesConfig, 'routes');

  3. #3
    flighty is offline Junior Member
    Join Date
    Aug 2009
    Posts
    4

    Default

    Thanks for this.

    But in the example you gave, it seems that you've hardcoded ":test/:test2"
    in the route. I need something that's more versatile, so that http://mydomain/any_variable/any_variable2 routes to the index action of the index controller.

    Can I use wildcards instead of ":test/:test2", so that the ini file would look like this?

    Code:
    routes.some_route.type = "Zend_Controller_Router_Route"
    routes.some_route.route = "*/*"
    routes.some_route.defaults.module = default
    routes.some_route.defaults.controller = index
    routes.some_route.defaults.action = index

  4. #4
    alokin is offline Senior Member
    Join Date
    Apr 2009
    Posts
    211

    Default

    No, test and test2 means that you'll get those params by names test and test2, and those params can hold any value you want, for example:
    Code:
    $test = $this->_request->getParam('test');
    $test2 = $this->_request->getParam('test2');
    Or, without rewrite rules, that URL will look something like this:
    http://yourdomain/index.php?test=foo&test2=bar

    Hope that's clear now...

Similar Threads

  1. The requested URL /Index/index.php was not found on this server.
    By pan in forum General Q&A on Zend Framework
    Replies: 15
    Last Post: 04-14-2012, 10:11 PM
  2. Getting dojo to work in non-index action
    By tebriel in forum General Q&A on Zend Framework
    Replies: 4
    Last Post: 02-04-2010, 08:49 PM
  3. Default layout not working for non index action
    By karandikargirish in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 02-17-2009, 01:42 PM
  4. making all pages go through the index controller / action
    By numerical25 in forum General Q&A on Zend Framework
    Replies: 1
    Last Post: 08-17-2008, 08:07 PM
  5. Action controller different from index
    By sebastian in forum Model-View-Controller (MVC)
    Replies: 13
    Last Post: 06-12-2008, 01:56 PM

Posting Permissions

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