Skip to content

Layer: Redirection Layer

Olivier Madre edited this page May 6, 2015 · 3 revisions

This page is still in a draft revision

Goal

The RedirectionLayer will help you handle all your redirections concerns easily. It can redirect to a specific URL or a specific route.

We're going to see some use cases to help you understand how it works, be more familiar with some "magic" characters to use in your config and finally have fun implementing all your redirections!

Use cases

In all the examples above, I'll use this basic sample yml file:

routes:
    home-old-way:
        route:
            pattern: "/home-old"
            methods: [ get ]
        dispatch:
            PyriteSessionFactory: { 'start': true }
            PyriteApplicationFactory:
                RedirectionLayer:
                    movetohome: '/home'
                Executor: [ RedirectController ]

When I'll refer to it, I'll only change the RedirectionLayer section for more clarity.

Basically, you have to know that the RedirectionLayer is waiting for the ResponseBag::ACTION_RESULT value in the responseBag to determine which action to run.

### 1 - Simple URL redirect

This is the simplest one. You want to redirect a specific route to another one. In our example, redirect home-old-way to home.

So what we're going to do inside the execute method of our RedirectController is to return a string refering to the value set in the config. In our case the config string is "redirect".

The RedirectController.php could look like this:

class RedirectController implements Executable
{
    /**
     * Simple redirect
     *
     * @param  Request     $request The HTTP Request
     * @param  ResponseBag $bag     The Bag shared by all Layers of Pyrite
     * @return string               result identifier (success / failure / whatever / ...)
     */
    public function execute(Request $request, ResponseBag $bag)
    {
        return 'movetohome';
    }
}

Then, as the RedirectionLayer match the param in the config, it will redirect to /home. In fact, this layer only add an header in the $responseBag containing the 'Location' property.

2 - Advanced URL redirect

You can do a redirect with a more advanced logic inside the controller. Let's see an example:

RedirectionLayer:
    success: '/profile'
    error: '/login-fail'

And inside your RedirectController

class RedirectController implements Executable
{
    ...
    public function execute(Request $request, ResponseBag $bag)
    {
        ...
        if ($user->isAuthenticated())
        {
            return 'success';
        } else {
            return 'error';
        }
    }
}

That's it!

### 3 - URL redirect to a route

If you want to redirect to another route you've already defined, then this section is for you.

You simply have to do:

RedirectionLayer: [ @profile ]

where profile is the name of your route. You can notice the use of a specific character @ which allows you to specify a route name.

To be continued

There's a lot more to do in order to improve this layer. For instance, allow the same syntax as simple redirects, with the redirect to route. Actually, we can only specify one route. We should also inject the routeCollection to allow some more advanced stuff to be done.

Clone this wiki locally