thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

setParentGroup($group) not behaving as expected? #244

Closed delboy1978uk closed 5 years ago

delboy1978uk commented 5 years ago

Maybe I am not understanding correctly, but if so i have a fix. I have a route /dragon and i want to add it to a group starting with a locale like en_GB, after which the route would become /en_GB/dragon. However, calling setParentGroup($group) has no effect.

route expected actual /en_GB/yes 200 200 /dragon 404 200 /en_GB/dragon 200 404

$request = ServerRequestFactory::fromGlobals(
    $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES
);

$router = new Router();

// create a group the normal way
$group = $router->group('/en_GB', function (\League\Route\RouteGroup $route) {
    $route->map('GET', '/yes', function (ServerRequestInterface $request) : ResponseInterface {
        $response = new Response;
        $response->getBody()->write('<h1>YES</h1>');
        return $response;
    });
});

// create a non grouped route
$route = $router->map('GET', '/dragon', function (ServerRequestInterface $request) : ResponseInterface {
    $response = new Response;
    $response->getBody()->write('<h1>Here be Dragons!</h1>');
    return $response;
});
// add the route to the group
$route->setParentGroup($group);

// get the response
$response = $router->dispatch($request);

I can send a pull reuest if indeed this behaviour is incorrect:

/**
     * Set the parent group
     *
     * @param \League\Route\RouteGroup $group
     *
     * @return \League\Route\Route
     */
    public function setParentGroup(RouteGroup $group) : self
    {
        $this->group = $group;
        $prefix = $group->getPrefix();
        $path = $this->getPath();
        if (strcmp($prefix, substr($path, 0, strlen($prefix))) != 0) {
            $path = $prefix . $path;
            $this->path = $path;
        }

        return $this;
    }

After which the expected routes now work.

delboy1978uk commented 5 years ago

https://github.com/thephpleague/route/pull/245

philipobenito commented 5 years ago

your PR is merged, will release soon

delboy1978uk commented 5 years ago

Many thanks amigo!!