0

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

I am trying to create a blog module where the user can post blog with custom slug like with URL structure.

I am having a problem on routing the URI 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 a blog and if it is not a post it should redirect to 404 page.

I already have a code on my router. I already have a code where it redirects to my view action handle

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

So, the code I have already accepts any slug and redirects to posts/viewPostHandler and pick a volt. But with this router it is affecting the default /:controller/:action router.

Like, index/loginSubmit is not working anymore.

Furtim
  • 23
  • 1
  • 4

1 Answers1

0

The order routes are registered matters.

If you load a concrete route ($this->router->addGet('/sample-url')) before a route with a slug that can match the concrete route ($this->router->addGet('/[a-z-]')) it will prioritize the slugged route.

If you reverse the registering order, so in the above example you load /sample-url last, it should work.

Relevant part of the documentation (search for "order"):

Since you can add many routes as you need using the add() method, the order in which routes are added indicate their relevance, latest routes added have more relevance than first added. Internally, all defined routes are traversed in reverse order until Phalcon\Mvc\Router finds the one that matches the given URI and processes it, while ignoring the rest.

  • Hi, I tried this already. And made the double wildcard last on my router configuration.. But, still the other links is broken. – Furtim Sep 04 '19 at 07:52
  • Could you show me the output of `$router->getRoutes()`? Are you sure they're loaded? –  Sep 04 '19 at 09:38