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 - Port RouteCollector::fullUrlFor() #2638

Merged
merged 2 commits into from
Apr 20, 2019

Conversation

l0gicgate
Copy link
Member

@l0gicgate l0gicgate commented Apr 20, 2019

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

The functionality is ported from #2493 and adds the ability to create full urls relative to a UriInterface.

It also changes the deprecation of the method urlFor() to pathFor() since it was determined that urlFor() is a more used terminology.

Example usage:

use Slim\App;
use Slim\Http\Factory\DecoratedResponseFactory;
use Slim\Http\ServerRequest;
use Slim\Middleware\ErrorMiddleware;
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();

/**
 * 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 ($routeCollector) {
    /** @var RouteInterface $route */
    $route = $request->getAttribute('route');
    $routeName = $route->getName();

    $uri = $request->getUri();

    /**
     * $fullyQualifiedUrl would be:
     * http://example.com/hello/world
     */
    $fullyQualifiedUrl = $routeCollector->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 20, 2019
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 98.527% when pulling e0346e6 on l0gicgate:4.x-FullUrlFor into 341e1bd on slimphp:4.x.

@l0gicgate l0gicgate merged commit d5497de into slimphp:4.x Apr 20, 2019
@l0gicgate l0gicgate mentioned this pull request Apr 20, 2019
7 tasks
@l0gicgate l0gicgate deleted the 4.x-FullUrlFor branch April 20, 2019 19:27
@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.

None yet

2 participants