wellrested / redirect
PSR-15 middleware for simple redirects
Installs: 2 981
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7
- psr/http-message: ~1.0
Requires (Dev)
- phpunit/phpunit: ^7
- wellrested/test: ^1
- wellrested/wellrested: >=3.1
This package is auto-updated.
Last update: 2024-10-12 07:32:11 UTC
README
PSR-15 handler for simple redirects
Same usage:
<?php use WellRESTed\Redirect\RedirectHandler; use WellRESTed\Message\Response; use WellRESTed\Server; $server = new Server(); $server->add($server->createRouter() ->register('GET', '/old-path', new RedirectHandler(301, '/new-path', new Response())) ); $server->respond();
When the handler is dispatched, it will return a response with the provided status code and location.
When using a dependency container, you may want to wrap RedirectHandler
with a tiny factory.
<?php use Pimple\Container; use Pimple\ServiceProviderInterface; use WellRESTed\Redirect\RedirectHandler; use WellRESTed\Server; class ServiceProvider implements ServiceProviderInterface { public function register(Container $c) { $c['redirect'] = $c->protect( function ($status, $location) { return new RedirectHandler($status, $location, new Response()); } ); $c['server'] = function ($c) { $server = new Server(); $server->add($c['app:notFoundHandler']); $server->add($server->createRouter() ->register('GET', '/', [ $c['app:rootHandler'] ]) ->register('GET', '/old-path', [ $c['redirect'](301, '/new-path') ]) ->register('GET', '/new-path', [ $$c['app:newThing'] ]) ->register('POST', '/login', [ $c['app:loginHandler'], $c['redirect'](303, '/') ]) ); return $server; }; } }