I'm looking for a solution to the problem I mentioned in the title.
I have ROUTER and I have an order I check whether the controller exists or not and I can not find a solution.
I did something like this
class Plugin_Router extends Zend_Controller_Plugin_Abstract {
public function routeStartup (Zend_Controller_Request_Abstract $request)
{
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
if (!$dispatcher->isDispatchable($request)) {
// Controller exists
// Exit ();
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('/:catid', new Zend_Controller_Router_Route('/:catid', array(
'module' => 'default' ,
'controller' => 'profile' ,
'action' => '' // Check your action and controller
)));
}
}
}
And it does not work there is another solution ?
It isn't very clear what this plugin is supposed to do. You've put your logic within routeStartup()
which happens before routing takes place. isDispatchable()
is always going to return false there, since the module/controller/action to dispatch hasn't yet been determined.
I would say move the logic to routeShutdown()
(which runs after routing), but the purpose of your code seems to be to add a new route if routing fails, which wouldn't achieve anything. You might need to rethink your approach.
You can change parameters in the request object before the dispatch occurs, which is probably what you want to do.
I found a half answer:
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$url = explode("/", substr($_SERVER['REQUEST_URI'], 1));
$path = APPLICATION_PATH . '/controllers/' . ucfirst(strtolower($url[0])) .
'Controller' . '.php';
if (!file_exists($path)) {
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute(':catid', new Zend_Controller_Router_Route(':catid', array(
'module' => 'default',
'controller' => 'profile',
'action' => 'index' // Check your action and controller
)));
}
}
}
If anyone has a working code on ZEND'd like to hear ....
After some testing, I can propose you this (this is inspired by the Dispatcher and Front Controller):
public function routeStartup(Zend_Controller_Request_Abstract $request){
{
//For feeding modules, controllers actions and parameters of $request
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->setParams(Zend_Controller_Front::getInstance()->getParams());
$router->route($request);
// get Module and Controller names
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
$controller = $request->getControllerName();
$default = $dispatcher->formatControllerName($controller);
$module = $request->getModuleName();
$controllerDirs = $dispatcher->getControllerDirectory();
$found = false;
$fileSpec = '';
if ($dispatcher->isValidModule($module)) {
if (class_exists($default, false)) {
$found = true;
} else {
$moduleDir = $controllerDirs[$module];
$fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $dispatcher->classToFilename($default);
if (Zend_Loader::isReadable($fileSpec)) {
$found = true;
}
}
}
if ($found) {
// The controller exists
...
}
}