Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slash at the end of url #74

Open
pagodzik opened this issue Nov 25, 2020 · 4 comments
Open

Slash at the end of url #74

pagodzik opened this issue Nov 25, 2020 · 4 comments

Comments

@pagodzik
Copy link

pagodzik commented Nov 25, 2020

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?

@daveh
Copy link
Owner

daveh commented Nov 26, 2020

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)

@pagodzik
Copy link
Author

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.

@diogenesjup
Copy link

the last solution of @pagodzik Works for me too

@dedelkov
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants