Documentation

Redirect attribute

Table of Contents 

Presentation

This attribute can be used to force a redirection, systematically preventing access to a controller or action. This can be useful during debugging, or to temporarily disable access to a portion of code.

Parameters

The attribute offers several parameters:

  • $url: (string) Redirection URL.
  • $var: (string) Name of the template variable containing the redirect URL.

Priority

To define the URL to which the user will be redirected, the attribute applies the following order of priority:

  1. If the $url parameter is defined, it is used.
  2. If the $var parameter is defined, and it contains the name of an existing, non-empty template variable, its content is used.
  3. If the temma.json file contains an x-security extended configuration, and this contains a redirect key, its content is used.

If no redirect URL is found, a 403 error is returned.

Configuration

To ensure that all Redirect attributes redirect to the same URL, simply define it in the temma.json file:

{
    "x-security": {
        "redirect": "/temporaryHomepage"
    }   
}

To ensure that the redirect URL is the same for the Auth, Method, Referer and Redirect attributes, simply define the redirect key in the x-security extended configuration in the temma.json file:

{
    "x-security": {
        "redirect": "/login"
    }   
}

Examples

use \Temma\Attributes\Redirect as TµRedirect;

// redirects all accesses to this controller
// to the URL defined in the temma.json file
#[TµRedirect]
class Login extends \Temma\Web\Controller {
    // ...
}
use \Temma\Attributes\Redirect as TµRedirect;

class Homepage extends \Temma\Web\Controller {
    // redirects to new home page
    #[TµRedirect('/newHomepage')]
    public function oldHome() { }
}
use \Temma\Attributes\Redirect as TµRedirect;

class Homepage extends \Temma\Web\Controller {
    // controller initialization: definition of
    // the 'homeRedir' template variable
    public function __wakeup() {
        $this['homeRedir'] = '/otherHomepage';
    }

    // redirection to the URL defined in the
    // 'homeRedir' template variable
    #[TµRedirect(var: 'homeRedir')]
    public function action1() {
        // ...
    }
}
Previous: Referer attribute helper
Next: urlize Smarty helper

Table of Contents