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.

I am developing a CMS but I am having a difficulty on my routing

Hello. I am a newbie in Phalcon PHP and I'm currently developing a CMS.

I am having a problem on routing the inputted address like I want to check first if the controller or action exists or not. If not, it should redirect to my action handler and check if this is a post and if it is not a post it should redirect to 404 page.

Currently, I have this code on my router to check

$router->add( '/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)', [ 'controller' => 'posts', 'action' => 'viewPostHandler', 'posts_category' => 1, 'posts_slug' => 2 ] );

But, it is catching my /index/logout and index/login

Please help me.



11.3k

I made a timekeeping tool, but you can use th strucxture as a CMS, maybe you can find some examples in it: https://github.com/Videles/PhalconTime Going to update it in the next few weeks as my current version has double the functionality. But it should give you a heads up already

Example URL: domain.name/news/phalcon-is-the-best

I am thinking that this would direct first on default as controller/action if it is not existing it should redirect to my action that I made to check if the URI is existing on my database. How can I do this?

Can you give an example of the type of URL you want to be considered a post?

Hi. It seems your router.php is empty.

I made a timekeeping tool, but you can use th strucxture as a CMS, maybe you can find some examples in it: https://github.com/Videles/PhalconTime Going to update it in the next few weeks as my current version has double the functionality. But it should give you a heads up already

Can anyone make this thread unsolved? I accidentally pressed the accept answer. Thanks

I deleted my reply that you accepted, but that doesn't seem to reset the [solved] status of the thread.

Can anyone make this thread unsolved? I accidentally pressed the accept answer. Thanks

The order you define the routes matter. If I remember correctly, routes will be checked in reverse-order.

So make sure you define the $router->add( '/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)', [ 'controller' => 'posts', 'action' => 'viewPostHandler', 'posts_category' => 1, 'posts_slug' => 2 ] ); first, and then the rest (like index/login).

If that doesn't work, I'd also try putting the catch-all route as the last, just in case i remember incorrectly.

You remember correctly. The first route you add gets checked last. So the catch-all should be added first, then routes that get progressively more and more specific.

The order you define the routes matter. If I remember correctly, routes will be checked in reverse-order.

So make sure you define the $router->add( '/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)', [ 'controller' => 'posts', 'action' => 'viewPostHandler', 'posts_category' => 1, 'posts_slug' => 2 ] ); first, and then the rest (like index/login).

If that doesn't work, I'd also try putting the catch-all route as the last, just in case i remember incorrectly.

I tried this already. But, it still catches all the default route like /:controller/:action

Just to be clear, I want this flow in my router:

  1. Check if the URI is an existing /:controller/:action if it is existing, route to the default router. If it is not, check other routers if it matched and route it there. If it doesn't matched any router, it should go to my router where it checks the URI(/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)), and route to the action where it checks to my database if it is an exisiting blog/post. If it is not, it should return 404 page.

The order you define the routes matter. If I remember correctly, routes will be checked in reverse-order.

So make sure you define the $router->add( '/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)', [ 'controller' => 'posts', 'action' => 'viewPostHandler', 'posts_category' => 1, 'posts_slug' => 2 ] ); first, and then the rest (like index/login).

If that doesn't work, I'd also try putting the catch-all route as the last, just in case i remember incorrectly.

edited Sep '19

You can gain more control over your flow if you define them in groups. I.e.: set the routes that you know that will have a /:controller/:action in one group (admin pages seem a good candidate for this); the news controller seems ideal to create another group that can use a conversor. The router will start to look more like:

$router->add(
    '/admin/users/profile',
    [ 
        'controller' => 'users',
        'action' => 'profile'
    ]
);

$router->add(
    '/news/{slug:[a-z\-]+}',
    [
        'controller' => 'news',
        'action' => 'show',
    ]
);

$route->convert(
    'slug',
    function ($slug) {
       return str_replace('-', '', $slug);
    }
);

So start by defining the main parts of your CMS, assign them routes and you'll have more control over them; you will be following the principle TellDontAsk. The routes in the example are taken from Routing in the documentation.

Thanks. Appreciated.

I deleted my reply that you accepted, but that doesn't seem to reset the [solved] status of the thread.

Can anyone make this thread unsolved? I accidentally pressed the accept answer. Thanks

And just remembered: in this book, the example application used by the author to learn Phalcon is a CMS. It's on discount now: https://www.packtpub.com/web-development/learning-phalcon-php