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

Add port if specified #205

Merged
merged 1 commit into from
Mar 25, 2025
Merged

Add port if specified #205

merged 1 commit into from
Mar 25, 2025

Conversation

harikt
Copy link
Member

@harikt harikt commented Mar 2, 2025

Hi all,

This is a fix for #153 , I am sorry that it is very late. At the time of the issue I didn't want to break the entire system. So this should be done very carefully.

I have added a test, reviewers please be careful.

@harikt harikt requested review from pmjones, jakejohns and koriym and removed request for jakejohns March 2, 2025 07:29
@harikt
Copy link
Member Author

harikt commented Mar 2, 2025

Example :

composer init
composer require aura/router laminas/laminas-diactoros
<?php
require_once __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();
    $response->getBody()->write("You asked for blog entry.");
    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 harikt requested a review from jakejohns March 2, 2025 07:48
@koriym
Copy link
Member

koriym commented Mar 24, 2025

@harikt Sorry it took so long. I ran your script and checked it too. I think it's fine!

@harikt harikt merged commit 63eb8f3 into 3.x Mar 25, 2025
25 checks passed
@harikt harikt deleted the withport branch March 25, 2025 05:56
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

Successfully merging this pull request may close these issues.

2 participants