views:

286

answers:

2

Below is a function defined in my Bootstrap class. I must be missing something fundamental in the way Zend does routing and dispatching. What I am trying to accomplish is simple: For any request /foo/bar/* that is not dispatchable for any reason try /index/foo/bar/. The problem I'm having is when the FooController exists I get Action "foo" does not exist. Basically, the isDispatchable is always false.

public function run() {
        $front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();
        $dispatcher = $front->getDispatcher();
        //$controller = $dispatcher->getControllerClass($request);
        if (!$dispatcher->isDispatchable($request)) {
            $route = new Zend_Controller_Router_Route(
                ':action/*',
                array('controller' => 'index')
            );
            $router = $front->getRouter();
            $router->addRoute('FallBack', $route);
        }
        $front->dispatch();
    }
A: 

Hi , if i understand your idea right would you try to use __call magic method ?? then use $this->_redirect(); to your default action for example

more info are here http://php.net/manual/en/language.oop5.overloading.php

UPDATE

if you opened Zend/Controller/Action.php on line 480

public function __call($methodName, $args)
    {
        require_once 'Zend/Controller/Action/Exception.php';
        if ('Action' == substr($methodName, -6)) {
            $action = substr($methodName, 0, strlen($methodName) - 6);
            throw new Zend_Controller_Action_Exception(sprintf('Action "%s" does not exist and was not trapped in __call()', $action), 404);
        }

        throw new Zend_Controller_Action_Exception(sprintf('Method "%s" does not exist and was not trapped in __call()', $methodName), 500);
    }

what i meant to do is to extend this class and override __call function exactly to be

classs My_Controller_Action extends Zend_Controller_Action{
   public function __call($methodName, $args)
        {
            ///// do your magic here ......redirection or logging the request or what ever 
        }
}

and make sure your controller extend your newly created class

class FooController extends My_Controller_Action
{
   public function indexAction()
    {
        // action body
    }
}

so if some how you called inexistent action __call will run this idea was about inexistent action only it won't work if the controller doesn't exist

tawfekov
I'm not sure which `__call` method you mean? Which class? More specifically, the controller class in this case does not exist. The reason I want to introduce a new route is so that I can start the loop over and have the router catch properly everything that follows `:action/`. I'm realizing however, that this cannot be done in bootstrap as the router and dispatcher are not ready yet. It think I may have to create a plugin or something.
Brett Pontarelli
@brett i had updated my answer hopefully my answer will help you
tawfekov
A: 

So this seems to work, but is not the best answer as it simply drops all the params. I might try shortly doing a forward to /index/[original uri] within the plugin:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
  protected function _initRoute() {
    $front = Zend_Controller_Front::getInstance();
    $routes = array(
      'FallBack' => new Zend_Controller_Router_Route(
        ':controller/:action/*',
        array('controller' => 'index', 'action' => 'index')
      )
    );
    $router = $front->getRouter();
    $router->removeDefaultRoutes();
    $router->addRoutes($routes);
    $front->setRouter($router);
    return $router;
  }

  protected function _initPlugin() {
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new My_Controller_Plugin_FallBack());
  }
    }

class My_Controller_Plugin_FallBack extends Zend_Controller_Plugin_Abstract {
  public function preDispatch(Zend_Controller_Request_Abstract $request) {
    $front = Zend_Controller_Front::getInstance();
    $dispatcher = $front->getDispatcher();
    $router = $front->getRouter();
    if (($router->getCurrentRouteName() == 'FallBack') &&
        !$dispatcher->isDispatchable($request)) {
      $request->setActionName($request->getControllerName());
      $request->setControllerName('index');
    }
  }
}
Brett Pontarelli