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

Custom Route And Make Friendly URL

I have 2 Module ! . Frontend vs Backend . Default is Frontend . My Route .

Important is I want hiding controller and action on url .

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

    $router = new Router();
    $router->setDefaultModule("frontend");

    $router->add('/:module[/]?:controller[/]?:action[/]?:params', array(
        'module'     => 1,
        'controller' => 2,
        'action'     => 3,
        'params'     => 4
    ));

    $router->add('/backend[:/]', array(
        'module'     => 'backend',
        'controller' => 'index',
        'action'     => 'index',
    ));

    return $router;
});

I want generate url like this .

https://localhost/backend <--- work

https://localhost/backend/ <--- not work

https://localhost/frontend/ <--- not work

Paginator url like this

https://localhost/page/1

Category url and pagination like this

https://localhost/category-name-is-here/

ex :

https://localhost/sports/ <---- category sports

https://localhost/sports/page/1 <---- category sports with paginattion

And URL friendly like this

https://localhost/i-want-to-make-url-friendly-like-this-p1.html <---- frontend-index-post

Someone help me



11.6k
    $router->removeExtraSlashes(true);
    ...
    $router->add("/myFriendlyURL/myFriendlyFuncName", array(
        'controller' => 'frontend',
        'action' => 'index',
    ));

about pagination, page numbers are passed as GET param, it's more difficult..not sure if that's could work:

    $router->add(
        "/([a-zA-Z0-9_-]+)/page/([0-9]+)",
        array(
            "controller" => "frontend",
            "action"     => "search",
            "category" => 1,
            "page" => 2,
         ));

you should be abble to grab the category and page in your search func using "$this->dispatcher->getParam("category" [or page]);"

then try to create your next/previous page link this :

    <?php echo $this->tag->linkTo("yourCategoryName/page/" . $page->next, "-next") ?>


8.6k
  $router->removeExtraSlashes(true);
  ...
  $router->add("/myFriendlyURL/myFriendlyFuncName", array(
      'controller' => 'frontend',
      'action' => 'index',
  ));

about pagination, page numbers are passed as GET param, it's more difficult..not sure if that's could work:

  $router->add(
      "/([a-zA-Z0-9_-]+)/page/([0-9]+)",
      array(
          "controller" => "frontend",
          "action"     => "search",
          "category" => 1,
          "page" => 2,
        ));

you should be abble to grab the category and page in your search func using "$this->dispatcher->getParam("category" [or page]);"

then try to create your next/previous page link this :

  <?php echo $this->tag->linkTo("yourCategoryName/page/" . $page->next, "-next") ?>

I was solved my problem ! Tks For Help !