I searched on Google and I got this code to check if the controller exists or not.
$front = Zend_Controller_Front::getInstance();
if ($front->getDispatcher()->isDispatchable($request)) {
}
But I do not know where I should put this code. What is $request?
I am in Boostrap.php. For me _initRoute, I need to check if there is a controller, if it will not, I will add a new route.
Updated after the first answer. I have several 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 route will display the content mydomain.com/{username} the same as mydomain.com/profile/index/username/{username}
But the problem is that when I type mydomain.com/{anything or any controller}, it is routed as I define it on Boostrap. So, I think I need to check that the controller exists or not if it does not follow the routes.
? _initPlugin, . , , .
boostrap:
<?php
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 {
}
}
}
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlugin() {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Plugin_MyX());
}
public function _initRoute() {
}
}