php-mvc
php-mvc copied to clipboard
Slash at the end of url
Hi
Is it possible that two urls: mysite.com/admin mysite.com/admin/ show the same? I did $router->add('admin/', ['controller' => 'Home', 'action' => 'index', 'namespace' => 'Admin']); $router->add('admin', ['controller' => 'Home', 'action' => 'index', 'namespace' => 'Admin']); but maybe there is simplier method?
Currently that is the way you'd have to do it - you could modify the router though if you like, adding something like the following inside the loop where the routes are matched in the match method:
$route = trim($route, '/');
(I'm working on an update to the framework that is more robust to things like this, so it includes this code to make routes work with and without slashes)
I did change which works for me - I think that we have trim rather url than route.
public function match($url)
{
foreach ($this->routes as $route => $params) {
$url = trim($url, '/').'/';
if (preg_match($route, $url, $matches)) {
...
And in index.php all route should '/' at the end.
the last solution of @pagodzik Works for me too
You can modify the regex in the add function in Core/Router.php to achieve this.
In this line:
$route = '/^' . $route . '$/i';
Add \/? before the $ to make the slash optional, like this:
$route = '/^' . $route . '\/?$/i';
Now you can add your route in the index.php without the ending slash, and it will resolve to both URL cases.