Aura.Router icon indicating copy to clipboard operation
Aura.Router copied to clipboard

Unable to generate URL with port number

Open smichaelsen opened this issue 8 years ago • 2 comments

I'm running an application at http://127.0.0.1:1244/ and I want to generate an absolute URI using the \Aura\Router\Generator. As I understand it I need to specify a ->host() for the route in order to get an absolute URI. But there is no way to set a port number. I can set the port number together with the host name ->host('127.0.0.1:1244') but then the \Aura\Router\Matcher won't match anymore.

Any ideas how to get around this problem?

PS: I'm using Aura.Router 3.1.0

smichaelsen avatar Sep 03 '17 08:09 smichaelsen

Why not create a new class with an extend of the existing class "Generator" and changing the constructor to add the port and use it in the buildUrl function https://github.com/auraphp/Aura.Router/blob/3.x/src/Generator.php#L184

ncou avatar Jun 22 '19 07:06 ncou

Hi there,

I spend some time debugging today on the issue. I found the reason I believe.

Either you can create your own rule and create one for the host and set it. You can read more from https://github.com/auraphp/Aura.Router/blob/4.x/docs/custom-matching.md .

image

The screenshot of code is https://github.com/auraphp/Aura.Router/blob/de25bae1e961a4a6fe3f44b8787798ce05a97871/src/Rule/Host.php#L60

Basically $request->getUri()->getHost() was not returning the port number. So appending the port will fix the issue.

Not sure if this is an issue with Aura itself. But I will leave this to @koriym and @pmjones decisions to close it.

Thank you.

Example for my fellow team mates.

<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';

// create a server request object
$request = Laminas\Diactoros\ServerRequestFactory::fromGlobals(
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);

// create the router container and get the routing map
$routerContainer = new Aura\Router\RouterContainer();
$map = $routerContainer->getMap();

// add a route to the map, and a handler for it
$map->get('blog.read', '/blog', function ($request) {
    $response = new Laminas\Diactoros\Response();
    $page = $request->getQueryParams()['page'] ?? '';
    $response->getBody()->write("You asked for blog entry $page.");
    return $response;
})->host('127.0.0.1:8000');

$map->get('index', '/', function () use ($routerContainer) {
    $response = new Laminas\Diactoros\Response();
    $generator = $routerContainer->getGenerator();
    $path = $generator->generate('blog.read');
    $response->getBody()->write("You asked for blog entry <a href=\"{$path}\">{$path}</a>.");
    return $response;
})->host('127.0.0.1:8000');

// get the route matcher from the container ...
$matcher = $routerContainer->getMatcher();

// .. and try to match the request to a route.
$route = $matcher->match($request);
if (! $route) {
    echo "No route found for the request.";
    exit;
}

// add route attributes to the request
foreach ($route->attributes as $key => $val) {
    $request = $request->withAttribute($key, $val);
}

// dispatch the request to the route handler.
// (consider using https://github.com/auraphp/Aura.Dispatcher
// in place of the one callable below.)
$callable = $route->handler;
$response = $callable($request);

// emit the response
foreach ($response->getHeaders() as $name => $values) {
    foreach ($values as $value) {
        header(sprintf('%s: %s', $name, $value), false);
    }
}
http_response_code($response->getStatusCode());
echo $response->getBody();

harikt avatar Jan 26 '22 11:01 harikt