We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

spaces in uri routing

Hi, Following cases all assume that the controller class does not exist.

Throws exception. /controller/method/something

Gets routed to default controller. /contr%20oller/method/something

So does this /controller/met%20hod/something

You can see it in sample app INVO.

Is this what you would expect ? I am on a Mac/Apache 2.2

Geza



98.9k

Phalcon\Mvc\Router provides default routes (https://docs.phalcon.io/en/latest/reference/routing.html#default-behavior), when an invalid URI is passed and these routes aren't matched, like in /contr%20oller/method/something, the router returns the default controller and action (index and index). You can change the not-found paths this way: https://docs.phalcon.io/en/latest/reference/routing.html#not-found-paths



1.4k

Thank you for your help ... according to what you say there if i get it correctly ;)

/controller/method/something

should NOT throw an exception but get routed to the default controller class just like /contr%20oller/method/something does. which is index and index.

Adding $router->notFound() does not seem to effect the behaviour of what happens and /controller/method/something still gives me "PhalconException: ControllerController handler class cannot be loaded"

//Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function(){
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri('/');
    return $url;
});

$di->set('router', function(){
$router = new \Phalcon\Mvc\Router();
//Set 404 paths
$router->notFound(array(
        "controller" => "index",
        "action" => "index"
    ));

$router->removeExtraSlashes(true);
$router->setDefaultController('index');
$router->setDefaultAction('index');

return $router;

});


1.4k

Adding this will solve this problem becuase I can catch the exception and rout a different way ...

$di->set('dispatcher', function() use ($di) {

    $eventsManager = $di->getShared('eventsManager');

    // If the path does not match a pre-defined controller, send it to a specific controller for handling
    $eventsManager->attach('dispatch:beforeException', 
        function($event, $dispatcher, $exception){
            switch ($exception->getCode()) {
                case Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(
                        array(
                            'controller' => '[CONTROLLER_NAME_HERE]',
                            'action'     => '[ACTION_NAME_HERE]',
                        )
                    );
                    return false;
            }                
        }
    );

    $dispatcher = new Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

It is from here: https://forum.phalcon.io/discussion/2029/routing-non-controller-urls-to-page-controller-for-database-stor

But is this the way to do it properly ...?