How to use expressions in security, routing, services and validation
Symfony comes with a powerful expression languageComponents. It allows you to add advanced custom logic to your configuration.
The Symfony framework makes good use of expressions in the following ways:
- Configuration Service ;
- Route matching conditions ;
- Security check (explained below) and Use allow_if for access control ;
- verify.
Create ³³For more information on using adverbial clauses: expressions, please refer to Expression syntax.
Security: Expressions use complex access control ¶
Accept roles other than ROLE_ADMIN
isGranted
Also. Accepts Expression
Object:
use Symfony\Component\ExpressionLanguage\Expression;// ... public function indexAction(){ $this->denyAccessUnlessGranted(new Expression( '"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())' )); // ...}
In this example, if the current user has ROLE_ADMIN
or the # of the current user object ##isSuperAdmin()The method returns true
Then access will be authorized (note: your user object may not have isSuperAdmin
method, this method only appears for this example).
This uses an expression. You can also learn more about the syntax for expressing languange, refer to Expression syntax.
In an expression, you can access various variables:
- user
- User object (or anon
, if not authenticated).
##roles - Array of roles owned by the user, including Role level (role level) but does not include the IS_AUTHENTICATED_*
attribute ( See function below). ##object
- isGranted
The object of the method (if any).
token - trust_resolver
Interface, its objects are: Perhaps you wish to use the
is_*function below instead.
is_authenticated
true
, if the user passes " Returns true if "Remember Me" or "Full" authentication is passed - for example, if the user is logged in. is_anonymous
isGranted
Use IS_AUTHENTICATED_ANONYMOUSLY
in the function. is_remember_me
IS_AUTHENTICATED_REMEMBERED
,see below. is_fully_authenticated
,see below. .