Symfony Flashcards
is symfony a full framework?
No it starts as a microframework then it builds up to be a frame work
how to make a new symfony project?
composer create-project symfony/skeleton name
what is index.php in the public folder?
it’s the front controller it’s a file that gets executed when you go to any url
what is a route?
configuration that defines the url of a page
what is a controller
a function that builds the content of the page
what is the rule of a controller method?
it must return a symfony response object
what is a slug?
it’s a url version of the title
what are the benefits of flex?
aliases
recipes
what are the aliases for flex?
it’s just a shortcut for the name of the bundle
what is a recipe for flex?
it is executed by flex to add files or create directories or even modify files so that the bundle works without anymore config
what is symfony.lock?
it’s a file managed by flex and keeps track of what recipes have been installed
what is bundles.php
it’s a file that has all the third party bundles that work with symfony
what is the AbstarctController
it’s a class that gives the controller shortcuts to methods and u don’t have to extend from it
how to return a template?
$this->render(templateName , array( title => $title))
what is the syntax of twig?
{{}} say something as it prints like a variable ora string or have logic in it like name = no ?? yes
{%%} do something
{##} comment
how to for loop in twig?
{% for comment in comments %}
{% endfor %}
how to inherit in twig?
{% extends ‘base.html.twig’ %}
what is the filter to get the length in twig?
comments | length
what happens when u tell twig to extend another template?
that u want to put your template inside of that template and the block are the holes where we can put our child template
how to put your content inside of a twig block?
{% block body %}
{% endblock %}
can u give the blocks in twig a default ?
yes and u can then override it
where can u use dump() from the profiler bundle?
ianywhere even twig {{ dump() }}
what is a pack?
it’s a single file that installs a bunch of other bundles in the same step
how to unpack a pack?
composer unpack name
where is the cache direcotry?
var/cache
why do we ut things in the public directory?
because they need to be accessed by the user browser
how should u include css or javascript?
using asset() function and it will give us more flexability to version our asset or cdn them
how to get url names in console?
php bin/console debug:router
how to generate url in twig?
{{ path() }}
how to generate a url for a wildcard?
{{ path() , { slug : value} }}
what happens when you override javascript or any block?
you completely override the parent so u must call {{ parent() }}
how to return a json response from the controller?
JsonResponse
$this->json
what is a servidce?
it’s an object that does something like generate urls or send emails
what is autowiring?
it’s a process by which symfony injects the object into the function by looking at the type hint u provided
how to get services from the console?
bin/console debug:autowiring
where does services live?
inside the container
who put the services inside the container?
bundles
what is a bundle?
it’s symfony plugin system and it lives in bundles.php as they provide services which are tools which means more bundles means more services
what is pipe raw in twig?
twig automatically escapes html in a variable and the pipe tells twig that it’s safe to use them
what are the aliases for services?
every service has a unique name and any service that has the same alias u can use any one of them
how to dump the configurations for a bundle?
php bin/console config:dump KnpMarkDownBundle
what is the use of a config for a bundle
change the way the bundle behaves
where does the config of a bundle live?
config/packages/file.yaml
does the name of the config file matter?
no just the root which is the key that is not indented t the start of the file as symfony automatically loads all files under packages directory
how to view a full list of the services in the container?
php bin/console debug:container –shiw-private
what does a servidce have in the contjainer?
a unique id
what class handles every request?
Kernel in Controller directory
what loads undles.php?
registerBundles() in kernel class
what is symfony composed of?
just routes and services
what loads the configuration files?
configContainer() in kernel class
what is the hierarchy of the config files?
packages gets loaded first then the environment specific but the env overrides anything before it
what is telda in yaml (~)?
it means null
what is the difference in cache between prod and env?
cache doesn’t get rebuilt in prod meaning when u change something in the front end it doesn’t take effect
how to build the initial files in cache?
php bin/console cache:warmup which makes the first request much faster
what should u do when u have a service that depends on other services?
don’t pass them as arguments but inject them in the constructor
what happens when u inject services in the constructor?
they get autowired u can autowire anything in debug:autowiring
how to find search for a service in the console?
php bin/console debug:container –show-private name
what library does symfony use for logging?
monolog
what are the benefits of using monolog?
u can have different channels that doe different things like logging to different files
how to manually tell symfony to pass a specific object while autowiring?
under services.yaml
services:
App\Services\MarkDownHelper:
arguments:
$logger: ‘@id for service’
how to pass the service and not the string in services.yaml?
use @ before like ‘@id’
what is the _defaults key in services.yaml?
it sets the default behavior of all services in this file?
what is autowire key in services.yaml?
services will be automatically injected
what is the App\ key in services?
make all classes inside src available as services
does the App\ mean that all the classes are instantiated?
no… symfony will not instantiate unless someone asked for the service
how many instances do u have for a service?
only one per request
what is the id of a service you create?
it’s equal to it’s name but mostly for bundles the have a snake naming like app.assets
what does bind: key under _defaults: key in services .yaml do?
it says when u find an argument like this pass this service like:
_default:
bind:
$markdownLogger:
‘@monolog.logger.markdown’
why do u put bind key under _defualts key?
to make the config project wide
what is the container?
it’s an object that holds all the services but it can also hold normal config values
how to define parameters for config?
under parameters key
parameters:
cache_adapter: ‘cache.apcu’
then u can reference it by :
‘%cache_adapter%’
where should u define your parameters?
u can do it anywhere but it’s better to make it centralized like in services.yaml
when does services.yaml load?
after packages then environment specific
how to fix that services.yaml loads last?
make a file called services_dev.yaml and override your parameters there
can we autowire scalar types like bool?
no because symfony doesn’t know it’s value
how to print the parameters in the console?
php bin/console debug:container –parameters
what is the debug parameter?
kernel.debug
where does autowiring happens?
the action of the controller or the constructor of the service and theconstructor is the place it’s meant to happen
why does autowiring work for controller actions?
just for convenience but it was meant to only work for the constructor as it’s used to instantiate services
what are public and private services?
in symfony 3 most services where public and u could get them using $this->get or use the container object to get the service but with symfony 4 in services.yaml we have public: false which means that most of the services we create are private and can’t use $this->get
what is faster private or public services?
private
what is dependency injection?
we pass objects and services through the parameters in the construction