Symfony 4 Cheat Sheet: by Via
Symfony 4 Cheat Sheet: by Via
[inner/left]join($join, $alias, $conditionType = null, $condition = null, setCacheable($trueFalse) Enable/disable result caching
$indexBy = null)
Persisting Entities
[and/or]where($where)
// get the entity manager that manages this entity
[add]groupBy($groupBy)
$em = $this->getDoctrine()->
[and/or]having ($having)
getManagerForClass(MyEntity::class);
[add]orderBy($field, $order = null)
// create a new entity
$entity = new MyEntity();
Doctrine Query
// populate / alter properties
getResult($hydrationMode = Retrieve a collection $entity->setName('Default Entity');
self::HYDRATE_OBJECT) // tell Doctrine you want to (eventually) save
All roles you assign to a user must begin with the ROLE_ prefix. Symfony\Component\Security\Core\Authorization\Authori
zationCheckerInterface
Authorization via security.yaml use
Symfony\Component\Security\Core\Exception\AccessDenie
# config/packages/security.yaml
dException;
security:
public function hello($name,
# ...
AuthorizationCheckerInterface $authChecker)
firewalls:
{
# ...
if (false === $authChecker-
main:
>isGranted('ROLE_ADMIN')) {
# ...
throw new AccessDeniedException('Unable to
access_control:
access this page!');
# require ROLE_SUPER_ADMIN for /admin/users*
}
- { path: ^/admin/users, roles:
// ...
ROLE_SUPER_ADMIN }
}
# require ROLE_ADMIN for /admin*
# Example of using annotation #
- { path: ^/admin, roles: ROLE_ADMIN }
use
No limit on amount of URL patterns. Each is a regular expression. First
Sensio\Bundle\FrameworkExtraBundle\Configuration\Secur
match will be used.
ity;
/**
Prepend the path with ^ to ensure only URLs beginning with the pattern
are matched. * @Security("has_role('ROLE_ADMIN')")
*/
# Create a new form with a default name # class TaskForm extends AbstractType
$form = $this->createFormBuilder($data) {
->add('dueDate', null, array( public function buildForm(
'widget' => 'single_text')) FormBuilderInterface $builder,
->add('save', SubmitType::class) array $options)
->getForm(); {
# Create a form with a non-default name # $builder
$form = $this->container->get('form.factory') ->add('dueDate', DateType::class, array(
->createNamedBuilder( 'widget' => 'single_text',
'form1', FormType::class, $data) 'label' => 'Due Date',
->add('dueDate', null, array( 'required' => false,
'widget' => 'single_text')) 'attr' => array('maxlength' => 10),
->add('save', SubmitType::class) 'constraints' => array(new Length(
->getForm(); array('max' => 10)))
))
Create a form from a class in a controller ->add('save', SubmitType::class);
}
# Create a form from a form class with a default name
public function configureOptions(
#
OptionsResolver $resolver)
$form = $this->createForm(TaskForm::class, $data,
{
array(
$resolver->setDefaults(array(
'action' => $this->generateUrl('target_route'),
'method' => 'GET',
'method' => 'GET',
));
));
}
# Create a form from a form class with a non-default
}
name #
$form = $this->container->get('form.factory')
->createNamed('form1', TaskForm::class, $data,
array(
'action' => $this->generateUrl('target_route'),
'method' => 'GET',
));
'action' => '' Where to send the form's data {{ form_rest(view, variables) }} Render all other fields
on submission (usually a URI)
{{ form_end (view, variables) }} Render end tag +
'allow_extra_fields ' => false Allow additional fields to be all other fields
submitted
{{ form_row(view.field) }} Render field label, error
'error_mapping' => Modify the target of a validation and widget
array('matchingCityAndZipCode' => error
{{ form_label (view.field, label, variables) }} Render field label
'city')
{{ form_errors(view.field) }} Render field error
'extra_fields_message' => 'This Validation error message if
additional fields are submitted {{ form_widget (view.field, variables) }} Render field widget
form should not contain extra fields.'
'inherit_data' => false Inhered data from parent form or {# render a label, but display 'Your Name' and add a "foo" class to it #}
not {{ form_label(form.name, 'Your Name', {'label_attr': {'class': 'foo'}}) }}
TwigBridge - Forms error() Runtime errors that do not require immediate action but
should typically be logged and monitored.
{{ form (view, variables) }} Render whole form
warning() Exceptional occurrences that are not errors.
{{ form_start (view, variables) }} Render start tag
notice() Normal but significant events.
{{ form_errors(view) }} Render global errors
info() Interesting events.
{{ form_row(view, variables) }} Render all the fields
debug() Detailed debug information. render($view, $parameters, $response = null) Render the template
and return a
use Psr\Log\LoggerInterface;
Response
public function index(LoggerInterface $logger) { json($data, $status = 200, $headers = array(), Encode data and
$logger->info('I just got the logger'); $context = array()) return a Json
} response
return $this->json($data);
Usage:
php bin\console command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-n, --no-interaction Do not ask any interactive question
-e, --env=ENV The environment name [default: "dev"]
--no-debug Switches off debug mode
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal
output, 2 for more verbose output and 3 for debug