Expressive/Mezzio middleware for URL Redirect handling
MIT License
This is README for version ^2.0 which only support Mezzio version 3 with php ^7.1.
For version ^1.0, , you can read at version 1.* readme which support ZF Expressive version 3 with php ^7.1.
For version 0.*, you can read at version 0.* readme which still ZF Expressive version 1 and 2 with php ^5.6|^7.0 support.
ExpressiveRedirectHandler is a package that contains Mezzio middleware for handling redirect that fit with mezzio-skeleton for following conditions:
RedirectResponse
is not registered in routing configFor example, we use RedirectResponse
instance in our Middleware:
use Laminas\Diactoros\Response\RedirectResponse;
// ...
$redirect = '/foo'; // may be a variable from GET
return new RedirectResponse($redirect);
if the passed $redirect
as url is a valid and registered in the routes, it uses default RedirectResponse
implementation, otherwise, it will redirect to default default_url
registered in config/autoload/expressive-redirect-handler.local.php
:
For example, we define:
<?php
return [
'expressive-redirect-handler' => [
'allow_not_routed_url' => false,
'default_url' => '/',
'options' => [
'exclude_urls' => [
// 'https://www.github.com/samsonasik/ExpressiveRedirectHandler',
], // to allow excluded urls to always be redirected
'exclude_hosts' => [
// 'www.github.com'
],
],
],
// ...
];
It means, we can't allow to make redirect to outside registered routes, whenever found un-registered url in routes, then we will be redirected to default_url. It also disable redirect to self, so you can't redirect to self.
For specific urls that exceptional ( allowed to be redirected even not registered in routes), you can register at exclude_urls
/exclude_hosts
options.
if you define exclude_urls/exclude_hosts options, which one of them is your own current url/host/domain, its your risk to still get "infinite" redirection loops. so, make sure exclude_urls/exclude_hosts is not your current own.
<?php
return [
'expressive-redirect-handler' => [
'allow_not_routed_url' => false,
'default_url' => '/',
'header_handler' => [
'enable' => true, // enable it!
'headers' => [
401 => '/login',
503 => '/maintenance',
],
],
],
// ...
];
Based on the configuration above, when header status code is 401 or 503, it will be redirected to its paired value.
composer require samsonasik/expressive-redirect-handler
Copy vendor/samsonasik/expressive-redirect-handler/config/expressive-redirect-handler.local.php.dist
to config/autoload/expressive-redirect-handler.local.php
and modify on our needs.
Open config/pipeline.php
and add:
$app->pipe(ExpressiveRedirectHandler\Middleware\RedirectHandlerAction::class);
at the very first pipeline records.
Contributions are very welcome. Please read CONTRIBUTING.md