Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Weird routing on index with special chars

Hello,

Just found a weird case with routing on my site.

If I visit the base url:

https://site.com

All works fine.

If I try an undefined controller or anything with / that is not defined, it 404s properly:

https://site.com/fdsfds/dfdsfds

https://site.com/bhdfds/sdsd

Defined routes work fine:

https://site.com/this-exists

However, any request to something undefined with a special char doesn't 404 but shows the index page:

https://site.com/fdsfds.com

https://site.com/[email protected]

https://site.com/sdfds.dc/sdfdsf

https://site.com/fdsfds.

These types all display the homepage. I want them to 404.

I'm using FactoryDefault.

My router:

$di->set('router', function(){

    $router = new \Phalcon\Mvc\Router();

    // camelize
    $router->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', [
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ])->convert('action', function($action) {
        return Phalcon\Text::lower(Phalcon\Text::camelize($action));
    });

    return $router;

}, true);

Dispatcher:

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

    // Create an EventsManager
    $eventsManager = new Phalcon\Events\Manager();

    $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([
                    'controller' => 'error404',
                    'action' => 'index'
                ]);
                return false;
        }
    });

    $dispatcher = new Phalcon\Mvc\Dispatcher();

    // Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;

}, true);

Any ideas?



7.0k
Accepted
answer

hi ty you must register $route->notFound()

Good luck



3.6k

Well once again, Phalcon has the solution built right in. Not sure how I missed that.

Thank you