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

router->getRewriteUri() issue

I have faced with a strange behaviour of getRewriteUri() method of the router.

instead of static url in paginator i put :

<li><?php echo $this->tag->linkTo([$this->router->getRewriteUri() . "?page=" . $page->next, "Next", "class" => "paginator", "id" => "next"]) ?></li>

insted of expected

/localhost/project/controller/search?page=3

i have got this one:

/localhost/project//controller/search?page=3  //  <-  note  two shashes instead just one !!!!

the url service is registered in common way :

$di->setShared('url', function () {
    $config = $this->getConfig();

    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
});

and $url->_base_uri points to the "/project/" as expected. Also, _url of _GET is "/controller" as expected

I tried to add in router.php , according router's documentation https://github.com/phalcon/cphalcon/blob/v3.4.0/phalcon/mvc/router.zep

$router = $di->getRouter();

// Define your routes here
$router->removeExtraSlashes(true);
$router->handle();

but still getting two slashes as described above. what's wrong ? Phalcon is 3.4

Thanks in advance .

Hi @napon the removeExtraSlashes() use a rtrim(url. '/'). Here the problem is your base_url you have to use like this "/project" without the last slash

Good luck



2.9k
edited Feb '19

Unfortunately, slashes are created by Phalcon, not by me. The project skeleton was created by phalcon tools.

I'm still waiting for the answer. Is it a bug in Phalcon ?

Hi @napon the removeExtraSlashes() use a rtrim(url. '/'). Here the problem is your base_url you have to use like this "/project" without the last slash

Good luck

well you can modify the config.php



2.9k
edited Feb '19

After consulting Phalcon documentation and source on Github I came up with a solution, but others are also encouraged to offer another solution :

In the beginning of the search action of the controller i just added

$this->view->setVar('pageLink', ltrim($this->router->getRewriteUri(),"/"));

and modified the search view

<li><?php echo $this->tag->linkTo(["$pageLink" . "?page=" .$page->before, "Previous", "class" => "paginator","id" => "previous"]) ?></li> 

Since tag->linkTo generate links by the url component and page link is assumed to be without a starting slash the ltrim() just cut the first slash after getRewriteUri()

Thanks for the idea Emilio! :)

well you can modify the config.php