Let’s say I’ve got:
$app->get(‘/go/’.‘{pageID}’…
And I want to make the default route, i.e.
$app->get(‘/’…
Equal to
/go/home
How would I do that ?
Let’s say I’ve got:
$app->get(‘/go/’.‘{pageID}’…
And I want to make the default route, i.e.
$app->get(‘/’…
Equal to
/go/home
How would I do that ?
You may name the route with setName, you can get that route from the router service (which came from the container) using getNamedRoute(), and finally add your new route (the alias) and assign to it the named route.
I don’t know if there is a fast way to do it:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App(['settings' => ['displayErrorDetails'=>true]]);
$container = $app->getContainer();
$app->get('/my_route', function($request, $response, $args) {
return $response->write('Hello');
})->setName('my_route');
$app->get('/', $container['router']->getNamedRoute('my_route'));
$app->run();
Interesting idea, a bit verbose as you say, but looks worth a try !
Can I send arguments with that ? e.g. if my route is /my_route/{param}
not just /my_route
?
Hi @devnull!
I tried to set up the route with arguments, but it didn’t work. I think that the nikic/fastroute component upon which are built the slim routes didn’t support “alias” routes. I guess that the argument-less example works because the callback didn’t depend on the route arguments and it gets called ok. But when I tried to set up arguments I found that the route didn’t receive them.
Sorry, maybe it wasn’t a good idea after all!