Results 1 to 3 of 3

Thread: Find out controller name BEFORE dispatcher call

  1. #1
    Nemesis666 is offline Junior Member
    Join Date
    Jan 2008
    Posts
    1

    Default Find out controller name BEFORE dispatcher call

    Hi,

    I'm trying to make a routing decition in my index.php based on some cookie information and on the controller, the visitor will be routed to.

    Example: I have a website with "static pages" (staticController) and some "restricted pages" (restrictedController). Further on I have (or not) a cookie telling me, if I can access the restriced pages. Now based on my cookie and the page, the visitor wants to see, I have to decide, where to route him. If he has no cookie and wants to see the restricted page, I will redirect to a special static page. If he has no cookie and wants to see a static page, it's ok. And so on.

    I want to know, what controller the router will choose, before the dispatch() of the front controller is being called. Is there a way to do this?

  2. #2
    Leif.Högberg is offline Member
    Join Date
    Aug 2007
    Location
    Sweden
    Posts
    52

    Default

    [PHP]
    <?php

    /** Zend_Controller_Plugin_Abstract */
    require_once 'Zend/Controller/Plugin/Abstract.php';

    class Foo_Controller_Plugin_MyPlugin extends Zend_Controller_Plugin_Abstract
    {
    /**
    * preDispatch() plugin hook --
    *
    * @param Zend_Controller_Request_Abstract $request
    * @return void
    */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    //the following functions will tell you where the visitor is heading
    $request->getModuleName();
    $request->getControllerName();
    $request->getActionName();

    //this will send the user elsewhere.
    $request->setModuleName('modname')
    ->setControllerName('controllername')
    ->setActionName('actionname')
    ->setDispatched(false);
    }
    }
    [/PHP]

    Then just register the plugin in your bootstrap
    [PHP]
    $controller = Zend_Controller_Front::getInstance();
    $controller->registerPlugin(new Foo_Controller_Plugin_MyPlugin());
    [/PHP]

    And you are ready to rock!

  3. #3
    sunspark is offline Junior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    8

    Default

    That's almost all that I need myself, I think.

    I want to put my Auth & ACL within a single preDispatch, so I don't have to repeat it for all my controllers.

    The problem I ran into was how to redirect to the login form when either fails (user isn't logged in or user does not have permission).

    Using
    Code:
    $request->setControllerName('controllername')
    appears to mostly work, but...

    1. How can I pass a $_GET variable as well (/controller/action?var=something)?

    2. How can I have the rest of my preDispatch skipped? Just use
    Code:
    return;
    ?

Similar Threads

  1. can't get AJAX call to controller .. HELP!!
    By rsearls in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 07-13-2010, 01:34 PM
  2. Front Controller can't find plugin
    By tixrus in forum Model-View-Controller (MVC)
    Replies: 6
    Last Post: 04-02-2010, 07:28 AM
  3. Call a controller from a controller (& using Smarty)?
    By kineus in forum General Q&A on Zend Framework
    Replies: 5
    Last Post: 06-09-2009, 06:19 PM
  4. Dispatcher fails to call non-default actions
    By dtrebbien in forum Installation & Configuration
    Replies: 0
    Last Post: 04-29-2009, 01:46 PM
  5. Controller cannot find my class
    By ehalber in forum General Q&A on Zend Framework
    Replies: 6
    Last Post: 03-08-2008, 10:33 AM

Posting Permissions

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