Closed waqar-akram-se closed 2 years ago
I'd suggest using something like this, if you can:
$map->get('users', '/users');
$map->get('users.page', '/users/page/{page}');
Or otherwise, if you cannot rewrite the URL, maybe something like this would work:
$map->get('users', '/users');
$map->get('users.page', '/users/?page={page}');
Or you can build your own custom rules. See examples and information in this documentation.
thanks @cxj
$map->get('users.page', '/users/?page={page}'); this route still not working. It's not matching with define route maybe due to
?
Hey there,
I am sorry for being late here. But I did tested to see if this is really happening.
But couldn't find the problem you are facing. Seems like a route naming issue.
<?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;
});
// 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();
I have tested this in 4.x for now as we are moving forward. But not much changes in core in made in 4.x.
So closing the issue for now.
Thank you.
Hi @cxj I am facing a issue when I define a route for pagination http://example.com/users?page=2 it doesn't work please help me how can I define such route. with optional params
Thanks