Zend Framework check if controller exists or not

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)) {
    // Controller exists
}

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


//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() {


    }


}
+3
2

, .

$request, , Zend_Controller_Request_Http. , - .

routeStartup . , Request. , dispatchLoopShutdown().

:

class Application_Plugin_Example
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        $front = Zend_Controller_Front::getInstance();
        if ($front->getDispatcher()->isDispatchable($request)) {
            // Controller exists
        }
    }
}

404, , ErrorHandler.

+3

. , - URL-:/u/: /user/: , ;)

, , - :

/controller-name/
/profile-controller-name/
... with
/:username
at the end of the list

, . , , "" : username.

0

All Articles