.
should be explicit and mean only one possible behavior: foo.bar
- for object properties, foo['bar']
for access to an array, foo.getBar()
for calling methods. use Symfony\Component\ExpressionLanguage\ExpressionLanguage; $language = new ExpressionLanguage(); echo $language->evaluate('1 + 1'); // echo 2 echo $language->compile('1 + 2'); // echo "(1 + 2)"
$language->evaluate('a.b', array('a' => new stdClass())); $language->compile('a.b', array('a'));
register()
method) $c->register('foo', 'Foo')->addArgument(new Expression('bar.getvalue()'));
service()
to get the service, and parameter
to get the value of the parameter: service("bar").getValue(parameter("value"))
<service id="foo" class="Foo"> <argument type="expression">service('bar').getvalue(parameter('value'))</argument> </service>
$this->get("bar")->getvalue($this->getParameter("value"))
allow_if
directive simplifies setting up access rules in your application: access_control: - { path: ^/_internal/secure, allow_if: "'127.0.0.1' == request.getClientIp() or has_role('ROLE_ADMIN')" }
/_internal/secure
for users who are not logged on from localhost
or who do not have administrator rights.request
, token
and user
are variables to which you have access, is_anonymous()
, is_authenticated()
, is_fully_authenticated()
, is_rememberme()
, and has_role()
- functions available in expressions when setting access rules.expression
function. {% if is_granted(expression('has_role("FOO")')) %} ... {% endif %}
@ Security
annotation /** * @Route("/post/{id}") * @Security("has_role('ROLE_ADMIN')") */ public function showAction(Post $post) { }
@ Security
will be part of version 3 of the bundle, which will be released before Symfony 2.4 /** * @Route("/post/{id}") * @Cache(smaxage="15") */ public function showAction(Request $request, Post $post) { $response = new Response(); $response->setLastModified($post->getUpdated()); if ($response->isNotModified($request)) { return $response; } // ... }
/** * @Route("/post/{id}") * @Cache(smaxage="15", lastModified="post.getUpdatedAt()") */ public function showAction(Post $post) { // ... }
info
, method
, sheme
), but some need more complex logic based on information from the request ( Request
object)condition
, which allows you to add any expression using the request
and routing context
variables: hello: path: /hello/{name} condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') =~ '/firefox/i'"
// hello if (0 === strpos($pathinfo, '/hello') && preg_match('#^/hello/(?P<name>[^/]++)$#s', $pathinfo, $matches) && (in_array($context->getMethod(), array(0 => "GET", 1 => "HEAD")) && preg_match("/firefox/i", $request->headers->get("User-Agent"))) ) { return $this->mergeDefaults(array_replace($matches, array('_route' => 'hello')), array ()); }
Expression
condition allows you to use expressions for validation: use Symfony\Component\Validator\Constraints as Assert; /** * @Assert\Expression("this.getFoo() == 'fo'", message="Not good!") */ class Obj { public function getFoo() { return 'foo'; } }
this
refers to the current validation object. # Get the special price if user.getGroup() in ['good_customers', 'collaborator'] # Promote article to the homepage when article.commentCount > 100 and article.category not in ["misc"] # Send an alert when product.stock < 15
Source: https://habr.com/ru/post/202058/
All Articles