alexdodonov / mezon-router

Small and fast router
https://twitter.com/mezonphp
267 stars 18 forks source link

I have a problem with the universal handler "*" when I use routes with parameters #11

Closed lolix-notepad closed 3 years ago

lolix-notepad commented 3 years ago

When I try for example this code:

$router = new \Mezon\Router\Router();
$router->addRoute('/hello/[a:name]', function($route, $parameters) {
    $name = $parameters['name'];
    echo "hello $name";
});

$router->addRoute('/*/', function() {
    echo "example text";
});

$router->callRoute($_SERVER['REQUEST_URI']);

And when I navigate to the route '/hello/world' I see the output for the universal handler "example text". I don't know if maybe I have a problem or error in my code. The thing I want it's create a website and the routes differents for my defined routes show a message or alert.

ghost commented 3 years ago

I have a similar issue! In my case, $this -> addRoute( '/pages/[s:test]/' , function(){echo "OK";} ); Works only for "/pages/s/"

ghost commented 3 years ago

I think the problem is with addParamRoute Method of RoutesSet Tarit. $this->paramRoutes[$requestMethod][$bunchCursor]['bunch'][$lastBunchSize + 1] = [ 'pattern' => $route, 'callback' => $callback ]; above part of your code sets $route in regex pattern while [s:test] is not a regex pattern for "any string";

ghost commented 3 years ago

When I try for example this code:

$router = new \Mezon\Router\Router();
$router->addRoute('/hello/[a:name]', function($route, $parameters) {
    $name = $parameters['name'];
    echo "hello $name";
});

$router->addRoute('/*/', function() {
    echo "example text";
});

$router->callRoute($_SERVER['REQUEST_URI']);

And when I navigate to the route '/hello/world' I see the output for the universal handler "example text". I don't know if maybe I have a problem or error in my code. The thing I want it's create a website and the routes differents for my defined routes show a message or alert.

This is because /*/ is considered to be static and overrides all dynamic roles while it must be considered dynamic.

lolix-notepad commented 3 years ago

How can I fix the error, please? And thanks for getting back to me.

alexdodonov commented 3 years ago

@lolix-kernel Hi! "*" route overrides all routes.

Looks like you have expected that it works in this way:

$router->addRoute('/hello/[a:name]', ...); // 1st
$router->addRoute('*', ...); // 2nd
$router->addRoute('/bye/[a:name]', ...); // 3rd

// for route /hello/joe/ will be called the first route
// for route /some-unexpected-route/ will be called the second route
// for route /bye/helen/ will be called the third route

Am I right?

alexdodonov commented 3 years ago

@lolix-kernel try version 1.3.0

I hope it will fix all your issues

alexdodonov commented 3 years ago

@sinakuhestani

I have a similar issue! In my case, $this -> addRoute( '/pages/[s:test]/' , function(){echo "OK";} ); Works only for "/pages/s/"

Could you please explain what do you mean?

This code works fine:

$router = new Router();
$router->addRoute('/pages/[s:test]/', function (string $route, array $params) {
       return $params['test'];
});

// test body
var_dump($router->callRoute('/pages/s/')); // outputs 's'
var_dump($router->callRoute('/pages/t/')); // outputs 't'
var_dump($router->callRoute('/pages/sssss/')); // outputs 'sssss'
var_dump($router->callRoute('/pages/1/')); // outputs '1'
lolix-notepad commented 3 years ago

@lolix-kernel try version 1.3.0

I hope it will fix all your issues

I tried the new version of the router and now my code works, thank you so much. By the way, how can I create a customize error 404 page with the router?

I have this line in the bottom of my code:

$router->addRoute('/*/', function() {
    echo "Error 404";
});

So I don't know if is there another better way for this. Thanks again.

alexdodonov commented 3 years ago

Hi! You can use $router->setNoProcessorFoundErrorHandler(function(string $route){echo "Error 404";}); for processing error if route handler was not found.

lolix-notepad commented 3 years ago

Thanks again, now I use the method that you said me:

$router->setNoProcessorFoundErrorHandler(function(string $route) {
    echo "Error 404";
}); 

Thank you so much again

alexdodonov commented 3 years ago

Looks like I can close this issue.

If you have another problems - feel free to open new ones.