So, I’ve been trying to find stuff online about it, but no where in the documentation is there anything regarding trailing slashes in URLs. Here’s my problem.
I want all URLs to end with a slash, and, potentially, redirect them if they don’t. Now, my code works with and without a slash, but I get two different results when I var_dump( $this->dispatcher->getParams() );
Here’s my code for the route
<?php
// for brevities sake, it's only the route
$router->add('/:controller/:action/:id/', array(
'controller' => 1,
'action' => 2,
'id' => 3
));
Now, that trailing slash I have there doesn’t really do anything, so I just left it. For the URL /user/view/13/, here’s the var_dump for $this->dispatcher->getParams():
array(2) {
[0]=>
string(2) "13"
[1]=>
string(0) ""
}
If I run the URL /user/view/13 (without the trailing slash), here’s the var_dump for
$this->dispatcher->getParams():
array(2) {
[0]=>
string(2) "13"
}
Essentially, is there a way I can get phalcon to end at the trailing slash, so that I get the same results? Or will I potentially have to write a custom Routing class? Any help would be appreciated!
For versions <= 0.7.0, you can add an optional / to the regular expression in each route:
Note: :id is not a valid placeholder in Mvc\Router instead of it you can use :int
Starting from 0.8.0, you can enable the automatic correction of trailing slashes: