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

4.x - Decouple FastRoute RouteParser #2640

Merged
merged 5 commits into from
Apr 21, 2019

Conversation

l0gicgate
Copy link
Member

@l0gicgate l0gicgate commented Apr 21, 2019

This is pull 6 out of 7 to complete the goals set in #2604

This decouples the FastRoute route parser from RouteCollector. It introduces a new interface RouteParserInterface.

The new interface implements the following methods:

  • RouteParser::relativePathFor()
  • RouteParser::urlFor()
  • RouteParser::fullUrlFor()

A new method on RouteCollectorInterface is introduced:

  • RouteCollectorInterface::getRouteParser()

Example Usage:

use Slim\App;
use Slim\Http\Factory\DecoratedResponseFactory;
use Slim\Http\ServerRequest;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Psr7\Factory\StreamFactory;

$responseFactory = new DecoratedResponseFactory(new ResponseFactory(), new StreamFactory());
$app = new App($responseFactory);
$routeCollector = $app->getRouteCollector();
$routeParser = $routeCollector->getRouteParser();

/**
 * The base app url would be for example:
 * http://example.com
 *
 * If you wanted to generate a fully qualified URL from within a route
 * you would do something like the following
 */
$route = $app->get('/hello/{name}', function ($request, $response, $args) use ($routeParser) {
    /** @var RouteInterface $route */
    $route = $request->getAttribute('route');
    $routeName = $route->getName();

    $uri = $request->getUri();

    /**
     * $fullyQualifiedUrl would be:
     * http://example.com/hello/world
     */
    $fullyQualifiedUrl = $routeParser->fullUrlFor($uri, $routeName, ['name' => 'world']);

    return $response;
});
$route->setName('test');

$request = new ServerRequest(ServerRequestFactory::createFromGlobals());
$app->run($request);

@l0gicgate l0gicgate added this to the 4.0 milestone Apr 21, 2019
@l0gicgate l0gicgate mentioned this pull request Apr 21, 2019
7 tasks
@coveralls
Copy link

coveralls commented Apr 21, 2019

Coverage Status

Coverage increased (+0.009%) to 98.544% when pulling 0c267ec on l0gicgate:4.x-DecoupleFastRouteRouteParser into f1b45ef on slimphp:4.x.

@l0gicgate l0gicgate merged commit e65a4dc into slimphp:4.x Apr 21, 2019
@l0gicgate l0gicgate deleted the 4.x-DecoupleFastRouteRouteParser branch April 21, 2019 21:45
@l0gicgate l0gicgate mentioned this pull request Apr 25, 2019
@vlakoff
Copy link
Contributor

vlakoff commented Apr 28, 2019

Just to point out relativePathFor() has been renamed to relativeUrlFor(), refs #2654.

@l0gicgate l0gicgate mentioned this pull request Aug 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants