class AccountRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Account::class);
}
}
class AccountRepository extends \Doctrine\ORM\EntityRepository
{
}
App\Repository\AccountRepository:
arguments:
- 'App\Entity\Account' # Entity class
factory: ['@doctrine', 'getRepository']
If you are building a traditional web application:
composer create-project symfony/website-skeleton my_project
If you are building a microservice, console application or API
composer create-project symfony/skeleton my_project
symfony/skeleton
и для традиционного сайта. $constraint = new Assert\Collection(
[
'fields' => [
'cf_sub_products' => [
new Assert\NotBlank(),
new Assert\Type('array'),
new Assert\All([
new Assert\Collection([
'fields' => [
"size" => [
new Assert\NotBlank(),
],
"cf_product_id" => [
new Assert\NotBlank(),
],
"enabled" => [
new Assert\NotBlank(),
],
"price" => [
new Assert\NotBlank(),
],
"sku" => [
new Assert\NotBlank(),
],
],
])
])
]
]
]
);
$violations = $validator->validate($body, $constraint);
'toEmail' => [
new Assert\NotBlank(),
new Assert\Type('array'),
new Assert\All([
'constraints' => [
new Assert\NotBlank(),
new Assert\Email(),
],
])
]
security.yml
security:
access_control:
- { path: ^/ }
RequestVoter.php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Security;
class RequestVoter implements VoterInterface
{
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* @inheritDoc
*/
public function vote(TokenInterface $token, $subject, array $attributes)
{
if (!$subject instanceof Request) {
return self::ACCESS_ABSTAIN;
}
$route = $subject->attributes->get('_route');
$routeRole = $this->getRoleByRoute($route);
if ($this->security->isGranted($routeRole)) {
return self::ACCESS_GRANTED;
}
return self::ACCESS_DENIED;
}
protected function getRoleByRoute(string $route): string
{
// ...
}
}
{% transchoice count with { '%count%': count } %}
%count% яблоко|%count% яблока|%count% яблок
{% endtranschoice %}
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class Model
{
/**
* @var string
*/
public $foo;
/**
* @var string
*/
public $bar;
/**
* @Assert\Callback()
*
* @param ExecutionContextInterface $context
* @param $payload
*/
public function validateFooBar(ExecutionContextInterface $context, $payload)
{
$fooViolations = $context->getValidator()->validate($this->foo, new Assert\NotBlank());
$barViolations = $context->getValidator()->validate($this->bar, new Assert\NotBlank());
if ($fooViolations->count() > 0 && $barViolations->count() > 0) {
$context->buildViolation('foo или bar должны быть заполнены')
->addViolation()
;
}
}
}
$violations = $this->get('validator')->validate($dto);
$dto = $this->get('serializer')->deserialize(
$request->getContent(),
DTO::class
);
public function editAction(DTO $dto)
{
}
CustomUserMessageAuthenticationException
- подкласс AuthenticationException
) и обрабатывает их по-свойму. $process = new Process("bin/console app:check-api $orderId > /dev/null 2>&1 &");
{% block password_widget %}
<div class="form-control">
{{ block('password_widget') }}
</div>
{% endblock %}
{% form_theme form _self %}
{% block password_widget %}
<div class="form-control">
{{ block('password_widget') }}
</div>
{% endblock %}
{% block password_widget %}
<div class="form-control">
{%- set type = type|default('password') -%}
{{ block('form_widget_simple') }}
</div>
{% endblock %}
FrameworkBundle/Resources/views/Form
лежат шаблоны для php-форм RequestAttributeValueResolver
отрабатывает раньше, соответственно до вашего резолвера дело не доходит.App\ArgumentResolver\UserIdResolver:
class: App\ArgumentResolver\UserIdResolver
tags:
- { name: controller.argument_value_resolver, priority: 101 }
class UserController {
/**
* @Rest\Get("/users/{userId}")
*/
public function getUserById(UserId $id): void {
var_dump($id);
die('1234');
}
}
App\ArgumentResolver\UserIdResolver:
class: App\ArgumentResolver\UserIdResolver
tags:
- { name: controller.argument_value_resolver, priority: 50 }
controller.argument_value_resolver
добавляется автоматически на основании интерфейса