Git Product home page Git Product logo

expressiveredirecthandler's Introduction

ExpressiveRedirectHandler

PHP version Software License Build Status Coverage Status Downloads

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:

  1. When the given url to RedirectResponse is not registered in routing config

For 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.

  1. When you want to redirect to specific url based on header status code

<?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.

Installation

  • Require via composer
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.

Contributing

Contributions are very welcome. Please read CONTRIBUTING.md

Credit

expressiveredirecthandler's People

Contributors

jbelien avatar samsonasik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

chacal88

expressiveredirecthandler's Issues

[Suggestion] Enable common Error Handlers with HTML pages

Needed:

  1. 401 Unauthorized -> redirect to login page

Nice to have:

  1. 402 Payment Required -> redirect to checkout page
  2. 503 Service Unavailable -> redirect to maintenance page

For discussion:

  1. 403 Forbidden -> for registration/update credentials
  2. 440 Login Timeout -> for re-login

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.