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

Passing or Forwarding Route

Hi,

I want to pre-check the current uri on routing to check exists pages. If doesnt exist the page, pass the matched route.

For examle uri: /hello-world

// Uri matched first
// I want to check `hello-world` on Pages db
// If the page found, run this route
// If doesnt exist, pass this route
$router->add('/{alias:[0-9a-z\-]+}', array(
    'controller' => 'pages',
    'action' => 'index',
    'alias' => 1
));

// If above doesnt matched, run this route
$router->add('/hello-world', array(
    'controller' => 'hello',
    'action' => 'world'
));

I hope I can tell. How can i do this?

Thanks

You can catch exception with dispatcher then redirect as required.
Use Phalcon\Events\Manager with dispatcher and beforeException event with a plugin class.
Example :
https://github.com/corentin-begne/phalconTool/blob/master/templates/project/app/config/services.php
https://github.com/corentin-begne/phalconTool/blob/master/templates/project/app/plugins/SecurityPlugin.php

edited Jul '17

Use beforeMatch method? To check if route with this alias exists perhaps?