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.
3Priority
To define the URL to which the user will be redirected, the attribute applies the following order of priority:
- If the $url parameter is defined, it is used.
- If the $var parameter is defined, and it contains the name of an existing, non-empty template variable, its content is used.
- 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:
[
'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:
[
'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() {
// ...
}
}