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

notFound problem when Phalcon\Mvc\Router(false);

I have this in my services.php:

$di->set('dispatcher', function () {
    //Create/Get an EventManager
    $eventsManager = new \Phalcon\Events\Manager();
    //Attach a listener
    $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
        //controller or action doesn't exist
        if ($event->getType() == 'beforeException') {
            switch ($exception->getCode()) {
                case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(array(
                        'controller' => 'index',
                        'action' => 'notFound'
                    ));

                    return false;
            }
        }
    });

    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    //Set default namespace to backend module
    $dispatcher->setDefaultNamespace("Vokuro\Controllers");
    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});
```php

and this in my routes.php:

```php

<?php

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

$router->add('/', array(
    'controller' => 'index',
    'action' => 'index'
));

$router->add('/login', array(
    'controller' => 'session',
    'action' => 'login'
));

$router->add('/registration', array(
    'controller' => 'session',
    'action' => 'signup'
));

return $router;

i have set Phalcon\Mvc\Router(false); to false, because I don't want session/login to be accessible, only /login for SEO purposes. It works great, however, the notfound redirect in the dispatcher in services.php is not forwarding the user to the IndexController and notFoundAction , but instead he is taken to the IndexController and IndexAction.

I have tried to add notFound in the routes like this:


<?php

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

$router->notFound(array(
    'controller' => 'index',
    'action' => 'notFound'
));

$router->add('/', array(
    'controller' => 'index',
    'action' => 'index'
));

$router->add('/login', array(
    'controller' => 'session',
    'action' => 'login'
));

$router->add('/registration', array(
    'controller' => 'session',
    'action' => 'signup'
));

return $router;

Now the /login and /registration works, but the homepage is redirected to the notFoundAction, which is wrong. the homepage should be /. Why is that? What should I change to allow homepage to work?

edited Jul '14

If you have this set, wouldn't that be why it is routing to index/index?

$router->add('/', array(
    'controller' => 'index',
    'action' => 'index'
));


37.0k
edited Jul '14

'If you have this set, wouldn't that be why it is routing to index/index?" No it's routing to index/notfound. That's a problem. A link like:

https://localhost/vokuro/

should point to IndexController and indexAction.

However, it's hitting IndexController and notFoundAction.

How to fix it?



37.0k
edited Jul '14

Sorry, you stated in the first post:

"It works great, however, the notfound redirect in the dispatcher in services.php is not forwarding the user to the IndexController and notFoundAction , but instead he is taken to the IndexController and IndexAction."

Then in follow up you state:

"should point to IndexController and indexAction. However, it's hitting IndexController and notFoundAction."

So a bit confused on what is and what isn't happening

the "vokuro/", assuming your baseuri is "vokuro/" is the same as "/" which is set to index/index in your route.

what happens if you type something like /blah/blah/blah?



37.0k
edited Jul '14

vokuro in the url is my base url defined in the bootstrap. so e.g. my https://localhost/vokuro is the same as https://vokuro.com. I have multiple projects in my wamp/www directory and vokurois one of them. I could set the redirects for the vhost like vokuro.dev but it's not needed for my development right now. Hope it's clearer after this explanation ;).

Any idea why homepage https://localhost/vokuro is hitting notFoundAction instead of the correct `indexAction'?

Did you solve this issue? If not could you post your .htaccess code here as well? Try to dump $_GET to see if you have any unexpected / unwanted values.