I searched on Google, and I got this code to check if a controller exists or not.
$front = Zend_Controller_Front::getInstance();
if ($front->getDispatcher()->isDispatchable($request)) {
// Controller exists
}
But I don’t know where I should put this code. What is $request?
I’m in Boostrap.php. I have _initRoute, I need to check if a controller doesn’t exist, if it doesn’t then I will add a new route.
Updated after first answer. I have some routes in Boostrap.php
public function _initRoute() {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute(
'username',
new Zend_Controller_Router_Route(':username',
array('controller'=>'profile',
'action'=>'index')
)
);
$router->addRoute(
'username/sets',
new Zend_Controller_Router_Route(':username/sets',
array('controller'=>'profile',
'action'=>'sets')
)
);
}
This Routes, will make mydomain.com/{username} show content same as mydomain.com/profile/index/username/{username}
But the problem is, when I type mydomain.com/{anything or any controller} , it routes as I define on Boostrap. So, I think, I need to check the controller is existing or not, if it doesn’t then do the routes.
Am I wrong? After first answer, I added the plugin, and put it under _initPlugin to register it. But look like it not work.
This is my boostrap file:
<?php
//Zend_View_Helper_PaginationControl::setDefaultViewPartial('paginator.phtml');
class Plugin_MyX extends Zend_Controller_Plugin_Abstract {
public function routeStartup(Zend_Controller_Request_Abstract $request) {
$front = Zend_Controller_Front::getInstance();
$dispatcher = $front->getDispatcher();
if (!$dispatcher->isDispatchable($request)) {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute(
'username',
new Zend_Controller_Router_Route(':username',
array('controller'=>'profile',
'action'=>'index')
)
);
$router->addRoute(
'username/sets',
new Zend_Controller_Router_Route(':username/sets',
array('controller'=>'profile',
'action'=>'sets')
)
);
} else {
// exist
}
}
}
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlugin() {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Plugin_MyX());
}
public function _initRoute() {
}
}
You should put that code in a Controller Plugin since the request object does not yet exist at bootstrap time.
The
$requestvariable in question is an object ofZend_Controller_Request_Http. This object is initially created when the front controller goes to dispatch a request.You could register a
routeStartupplugin and place the code there. That would be the earliest point at which you can use the Request object. All controller plugin chains will pass the request object to your plugin except fordispatchLoopShutdown().Here is sample plugin code:
If you are trying to just handle 404 errors, this is what the ErrorHandler plugin can be used for this purpose.