Redirect attribute


1Presentation

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.


2Parameters

The attribute offers several parameters:

  • $url: (string) Redirection URL.
  • $var: (string) Name of the template variable containing the redirect URL.
  • $referer: (bool) false by default. If true, and $url and $var are not set, the HTTP REFERER header is used as the redirection URL.

3Priority

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 $referer parameter is set to true, and the HTTP Referer header exists and is non-empty, it is used.
  4. If the etc/temma.php 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.


4Configuration

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

<?php

return [
    '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 etc/temma.php file:

<?php

return [
    'x-security' => [
        'redirect' => '/login'
    ]
];

5Examples

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

// redirects all accesses to this controller
// to the URL defined in the 'etc/temma.php' 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() {
        // ...
    }
}
use \Temma\Attributes\Redirect as TµRedirect;

class Homepage extends \Temma\Web\Controller {
    // redirects to the HTTP referer, or to the
    // configuration URL if the referer is missing
    #[TµRedirect(referer: true)]
    public function action1() {
        // ...
    }
}