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

Unable to generate URL with port number #153

Open
smichaelsen opened this issue Sep 3, 2017 · 2 comments
Open

Unable to generate URL with port number #153

smichaelsen opened this issue Sep 3, 2017 · 2 comments

Comments

@smichaelsen
Copy link

smichaelsen commented Sep 3, 2017

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

@ncou
Copy link

ncou commented Jun 22, 2019

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

@harikt
Copy link
Member

harikt commented Jan 26, 2022

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

$request->getUri()->getHost(),

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants