TooBasic: Using Redirections

What is a redirection?

A redirection is a mechanism inside TooBasic that allows controllers to check some conditions and based on that decide if it should continue or redirect the user to a different page. Imagin your site manages users and there are some controllers that can be accessed only when there's a user session active. For example, if a user want to access its own profile, it's obvious that it must be logged in, otherwise, the page should faild and redirect it to a login page, or perhaps the landing page.

TooBasic provides a rather simple but flexible mechanism to capture these events and redirect them to the right place, and we're going to explain how to use it based on the given example.

Configuration

For our example we are going to suppose we have a controller called my_profile that allows a user to review its profile information and a controller called login through which a user may sign-in. In our site, whenever a condition of a not-logged-in user is reached, we should send such user to a login page, and to achieve that we're going to define a redirect condition (a.k.a redirector) called login. Open your site's configuration file at ROOTDIR/site/config.php and add something like this:

$Defaults[GC_DEFAULTS_REDIRECTIONS]['login'] = [
    GC_AFIELD_ACTION => 'login'
];

This simple step will configure a new redirector that redirects to our login controller.

Checking conditions

Now that we have our redirector configured, we need to check when it must be used and for that we're going to edit our controller my_profile (it might by at ROOTDIR/site/controllers/my_profile.php) and add a public method called checkRedirectors(). Something like this:

<?php
class MyProfileController extends \TooBasic\Controller {

    . . .

    public function checkRedirectors() {
        $redirector = false;
        if(!$this->model->session->isLoggedIn()) {
            $redirector = 'login';
        }
        return $redirector;
    }
    protected function basicRun() {

    . . .
}

Here we are supposing there's a model called Session that provides all the functionality to determine if a user is logged in or not. In the case there's no user logged in, this new method returns the name of a redirector configuration to be processed.

For example, if a URL like this one is called with no user logged in:

http://www.example.com/mysite/?action=my_profile

I will be automatically redirected to:

http://www.example.com/mysite/?action=login&redirectedfrom=%2Fmysite%2F%3Faction%3Dmy_profile

And as you can see, there's a way to know where it comes from in any case it must be re-redirected after logging-in.

Complex redirectors

Beside GC_AFIELD_ACTION, redirector's configurations may accept some other parameters that we're going to explain in this section.

Parameters

Let's say your login page is capable of displaying a specific message when a user arrives to it after a redirection saying things like "You got here because 'reasons'.". To do that you may search for something in the URL parameter redirectedfrom or write something like this:

$Defaults[GC_DEFAULTS_REDIRECTIONS]['login'] = [
    GC_AFIELD_ACTION => 'login',
    GC_AFIELD_PARAMS => [
        'showmessage' => 'reasons'
    ]
];

In this way, when a user gets redirected, it may end up with this URL:

http://www.example.com/mysite/?action=login&showmessage=reasons&redirectedfrom=%2Fmysite%2F%3Faction%3Dmy_profile

Layout

Layout allows a redirector to set a specific layout when changing the URL. For example:

$Defaults[GC_DEFAULTS_REDIRECTIONS]['login'] = [
    GC_AFIELD_ACTION => 'login',
    GC_AFIELD_LAYOUT => 'errorlayout',
    GC_AFIELD_PARAMS => [
        'showmessage' => 'reasons'
    ]
];

With this a user may end up at:

http://www.example.com/mysite/?action=login&layout=errorlayout&showmessage=reasons&redirectedfrom=%2Fmysite%2F%3Faction%3Dmy_profile

results matching ""

    No results matching ""