Symfony Flashcards

1
Q

is symfony a full framework?

A

No it starts as a microframework then it builds up to be a frame work

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to make a new symfony project?

A

composer create-project symfony/skeleton name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is index.php in the public folder?

A

it’s the front controller it’s a file that gets executed when you go to any url

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a route?

A

configuration that defines the url of a page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is a controller

A

a function that builds the content of the page

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the rule of a controller method?

A

it must return a symfony response object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is a slug?

A

it’s a url version of the title

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what are the benefits of flex?

A

aliases

recipes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what are the aliases for flex?

A

it’s just a shortcut for the name of the bundle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a recipe for flex?

A

it is executed by flex to add files or create directories or even modify files so that the bundle works without anymore config

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is symfony.lock?

A

it’s a file managed by flex and keeps track of what recipes have been installed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is bundles.php

A

it’s a file that has all the third party bundles that work with symfony

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is the AbstarctController

A

it’s a class that gives the controller shortcuts to methods and u don’t have to extend from it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how to return a template?

A

$this->render(templateName , array( title => $title))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is the syntax of twig?

A

{{}} say something as it prints like a variable ora string or have logic in it like name = no ?? yes

{%%} do something

{##} comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to for loop in twig?

A

{% for comment in comments %}

{% endfor %}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

how to inherit in twig?

A

{% extends ‘base.html.twig’ %}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what is the filter to get the length in twig?

A

comments | length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what happens when u tell twig to extend another template?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

how to put your content inside of a twig block?

A

{% block body %}

{% endblock %}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

can u give the blocks in twig a default ?

A

yes and u can then override it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

where can u use dump() from the profiler bundle?

A

ianywhere even twig {{ dump() }}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

what is a pack?

A

it’s a single file that installs a bunch of other bundles in the same step

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

how to unpack a pack?

A

composer unpack name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
where is the cache direcotry?
var/cache
26
why do we ut things in the public directory?
because they need to be accessed by the user browser
27
how should u include css or javascript?
using asset() function and it will give us more flexability to version our asset or cdn them
28
how to get url names in console?
php bin/console debug:router
29
how to generate url in twig?
{{ path() }}
30
how to generate a url for a wildcard?
{{ path() , { slug : value} }}
31
what happens when you override javascript or any block?
you completely override the parent so u must call {{ parent() }}
32
how to return a json response from the controller?
JsonResponse $this->json
33
what is a servidce?
it's an object that does something like generate urls or send emails
34
what is autowiring?
it's a process by which symfony injects the object into the function by looking at the type hint u provided
35
how to get services from the console?
bin/console debug:autowiring
36
where does services live?
inside the container
37
who put the services inside the container?
bundles
38
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
39
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
40
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
41
how to dump the configurations for a bundle?
php bin/console config:dump KnpMarkDownBundle
42
what is the use of a config for a bundle
change the way the bundle behaves
43
where does the config of a bundle live?
config/packages/file.yaml
44
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
45
how to view a full list of the services in the container?
php bin/console debug:container --shiw-private
46
what does a servidce have in the contjainer?
a unique id
47
what class handles every request?
Kernel in Controller directory
48
what loads undles.php?
registerBundles() in kernel class
49
what is symfony composed of?
just routes and services
50
what loads the configuration files?
configContainer() in kernel class
51
what is the hierarchy of the config files?
packages gets loaded first then the environment specific but the env overrides anything before it
52
what is telda in yaml (~)?
it means null
53
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
54
how to build the initial files in cache?
php bin/console cache:warmup which makes the first request much faster
55
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
56
what happens when u inject services in the constructor?
they get autowired u can autowire anything in debug:autowiring
57
how to find search for a service in the console?
php bin/console debug:container --show-private name
58
what library does symfony use for logging?
monolog
59
what are the benefits of using monolog?
u can have different channels that doe different things like logging to different files
60
how to manually tell symfony to pass a specific object while autowiring?
under services.yaml services: App\Services\MarkDownHelper: arguments: $logger: '@id for service'
61
how to pass the service and not the string in services.yaml?
use @ before like '@id'
62
what is the _defaults key in services.yaml?
it sets the default behavior of all services in this file?
63
what is autowire key in services.yaml?
services will be automatically injected
64
what is the App\ key in services?
make all classes inside src available as services
65
does the App\ mean that all the classes are instantiated?
no... symfony will not instantiate unless someone asked for the service
66
how many instances do u have for a service?
only one per request
67
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
68
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'
69
why do u put bind key under _defualts key?
to make the config project wide
70
what is the container?
it's an object that holds all the services but it can also hold normal config values
71
how to define parameters for config?
under parameters key parameters: cache_adapter: 'cache.apcu' then u can reference it by : '%cache_adapter%'
72
where should u define your parameters?
u can do it anywhere but it's better to make it centralized like in services.yaml
73
when does services.yaml load?
after packages then environment specific
74
how to fix that services.yaml loads last?
make a file called services_dev.yaml and override your parameters there
75
can we autowire scalar types like bool?
no because symfony doesn't know it's value
76
how to print the parameters in the console?
php bin/console debug:container --parameters
77
what is the debug parameter?
kernel.debug
78
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
79
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
80
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
81
what is faster private or public services?
private
82
what is dependency injection?
we pass objects and services through the parameters in the construction
83
what should you do when you want to autowire a class that is not a service?
``` first you find it's id by using debug:container name then u put it in the constructor then u bind it by using the class name ``` Nexy\Client: '@id'
84
what are the two ways of binding?
u can use a variable name or a class name or interface
85
what happens when you put the class name: @id directly under services and not under bind?
a new service is registered with that name as an id
86
how does symfony know which object to pass when u create ur own services?
it just looks up the type hint in the container with that exact id
87
what are environment variables?
they are variables that are set in the operating system and then can be read by your code and we can get them by using getenv() or $_SERVER[]
88
how can we read environment variables in yaml?
'%env(NAME)%'
89
how to print environment variables in the console?
php bin/console about
90
where does environment variables live?
in .env during developmentand it doesn't get commited
91
what can we do when we don't find .env during development?
we copy .env.dist and make a .env
92
why should we use .env in production?
because it's slow
93
what is the limitation of an env variable?
they are only string values
94
how can u fix the limitation of env variable?
by casting it like | '%env(bool:NAME)%'
95
what are some prefixes while setting env variables under parameters?
u can set a default env value under parameters resolve: resolve parameters , like %foo% if they are inside environment variable file: read the contents of a file base64: base64_deode they are good with line breaks or special characters constant: read php constants json: json_decode and u can chain all of these like: json:file: and u can write ur own environment variable
96
what are setter injections?
it's a function used to set an object that is optional to the class meaning that the class will work even if it doesn't have that object like a logger setLogger() if($this->logger)
97
how to autowire a setter injection?
by using /**@required*/ above the setter will make symfony make an object of logger before the other object is passed
98
how to make a command using maker bundle?
php bin/console make:command name:name
99
how does symfony know that we added a new command?
thanks to autoconfigure all the services are registered and wehn we extend from command class symfony knows that it should register it as a command
100
what does each command have?
arguments : which are strings passed after the command | options: prefixed with --
101
does symfony have a database layer?
no it depends on doctrine
102
what is a symfonystyle class?
it's a class that helps with dealing with input and output to the console
103
how to create database in console?
php bin/console doctrine:database:create
104
what is doctrine?
an ORM
105
what is an ORM?
Object Relational Mapper
106
what is an Object relation mapper?
it means that each table in the database will have a corresponding class in our code which means if we want to create an article table we will have to create an article class
107
what is an entity to doctrine?
it's the name that doctrine gives to the classes that are saved in the database
108
how how make an entity in the console?
php bin/console make:entity
109
what is an entity?
it's a normal php class that can be saved in the database
110
what are field types in doctrine?
they are types that map the mysql data types like string to varchar
111
what tells doctrine that this class is an entity?
the annotations above the class
112
how to make a migration for the entity?
php bin/console make:migration
113
where does maker look when it creates a migration?
it looks at the database and the entity
114
how to run the migration?
php bin/console doctrine:migrations:migrate
115
how to know the status of the migrations in the console?
php bin/console doctrine:migrations:status
116
how does the migrations work?
it created a table in the database called migration_versions then the first time we ran doctrine:migrations:migrate it executed the migration and inserted a new row in the table with the version number which is the date and when we tried to run it a second time it looked at the table for that version in the code and it was already there so it knew that it shouldn't run it again
117
how to tell doctrine to save to the database?
just create an object from the entity u want to save and tell doctrine to save it
118
why does entity setter methods return $this?
so u can be able to chain the function calls
119
what is the service that saves to and gets from the database?
EntityManager from doctrine bundle or | EntityManagerInterface
120
what are the two steps to saving to the database?
persist() and flush()
121
what is the difference between persist and flush?
persist tells doctrine u just want to save it but the insert query is not done yet and then flush saves it to the data base
122
what happens after u call flush?
the object is created and u have a new id u don't need to go fetch the object from the database to get it's id
123
what is the first thing to do when u want to fetch from the database?
get the entity Repository
124
what does doctrine return when u query for something?
an object not an array and this is the whole point of doctrine as we are saving and fetching objects not rows
125
how to create a 404 in the controller?
throw $this->createNotFoundException this is a trait and it''s special because it generates 404
126
what happens when u say article.title in twig?
it looks for a property and if it has a getter it will use the method
127
what do u need to extend twig like make a filter?
a twig extension
128
how to create a twig extension?
bin/console make:twig-extension
129
how extensions should u make?
just one and put all your custom functions there
130
what is the problem when u extend twig app extension?
services injected in the constructor will be instantiated even if you don't use them because twig need to know about all of it's functionality even if u don't use them
131
how to fix the problem of extending the app extension?
by implementing ServiceSubscriberInterface and inject ContainerInterface as when u implement that interface it get injected automatically so the constructor must have that type hint available and in the function of getSubscribedServices return the class that u want to autowire and symfony makes it public so u can get it by $this->container->get() so instead of having the object u have a container that has the object and only used it in the function that u need
132
what is the mini container name?
ContainerInterface as it doesn't hold all the services just a subset of them
133
where should u use a service subscriber?
Twig Extensions Event Subscribers Security Voters
134
what should we use to find something by order?
$repository->findBy( [] [name => DESC])
135
what should we pass to findBy to get everything?
in the criteria pass an empty array
136
how is the entity and repository connected?
by the annotation ORM\Entity(repositoryClass) above the entity
137
what is the language of querying with doctrine?
Doctrine Query Language or DQL
138
what are the way of writing DQL?
write a DQL string or use a QueryBuilder
139
what is the query builder?
it's an object oriented builder that helps write the DQL
140
how should u order the functions of the query builder?
u can make them in any order it doesn't matter
141
what is the difference between where and andwhere in the query builder?
where will remove any other where clauses u may have added but andWhere is safe to use
142
should u use where or andwhere?
andwhere
143
what is a prepared statement?
that instead of hacking the value in the string by concatenation u use setParamets to se the value and the value is referenced by :val as that prevent SQL injection
144
what is the parameter u give to the querybuilder?
just an alias to the entity u r querying for
145
can u use Or in the andwhere statement?
yes
146
what do u always call at the end of the querybuilder?
getQuery()
147
what do u call to get an array of the objects?
getResult()
148
what do u call in querybuilder to get only one result?
getOneOrNullResult()
149
what happens when u make:entity on an existing one?
u update the existing one
150
how to set a default value for a field in the database?
b y giving it a default value in php private $heatCount = 0
151
what is the concatenation operator in twig?
~
152
should u use concatenation to concatenate images/imageFileName from database?
no create a method in the entity and use it
153
how to inject an entity inside a controller action?
type hint it like Article $article and send the slug or an id and symfony will try to find an object that corresponds to what u send
154
what is a race condition?
when there is a time when u fetch and save to the database that in this time the old value might change and 10 other might have saved to the same field
155
how to update an object in the database?
flush
156
do we call persist when we update an object?
no because when doctrine fetches the object it already knows to persist it and when u call flush it just updates it
157
when should u put logic related to an entity?
when it's simple just put ion the entity class but not the controller instead of a service
158
should u keep all setter and getter methods in the entity?
no u should replace some of them with a more descriptive methods like instead setHeartCount replace it with incrementHeartCount
159
how to make a fixture in the console?
php bin/console make:fixtures
160
how many fixture classes should u make?
one per entity or a group of related entity
161
what is the idea behind fixtures?
1 - we write code to create and save objects | 2- run a fixture command to execute all of our fixture classes
162
what is the command to load all fixtures?
php bin/console doctrine:fixtures:load
163
what is the ObjectManager?
it's an interface that implements entitymanager
164
what is a good trick to use when u want to create a fixture?
create an abstract BaseFixture Class and make an abstract method called loadData that takes ObjectManager implement two other methods load which must be implemented because it's a fixture and make it call loadData and make a private instance for the ObjectManager and then make another method called callMany which takes 3 arguments $className , $count , callable $factory make a for loop and inside it $entity = new $className(); factory($entity , $i) which is a method that gets called for every entity that we make then persist the entity and add a reference to that entity $this->addReference($className.'_'$i , $entity); this line helps us to reference one fixture class from another fixture class after all that go extend from base fixture and implement it's method loadData
165
what is stofdoctrineextensionbundle?
it adds many features like sluggable or blameable or loggable or timestampable
166
what are the types of repositories for any bundle?
contrib and main where contrib is not monitored by symfony and people can contribute to it
167
how does sluggable feature work behind the scenes?
by creating an event subscriber?
168
what else could u use event subscribers for like sluggable?
add an index in the database everytime u do a new insert
169
what should u do when a migrations fails?
u should drop the database and start over as it's now in an unstable state because if a migration has more than one sql statement and the second or third fail the first one now will run twice when u try to rerun the migration
170
how to drop the database using console?
php bin/console doctrine:database:drop
171
what should u do when u want timestampable?
u should use the trait timestampableentity and don't create the fields yourself
172
how to create a relationship between entities?
php bin/console make:entity and when asked for the field name write the other entity name like article and when asked for the type write relation
173
what happens when u have an entity that relates to an array of another entity?
u must set the initial property in the constructor to be equal to a new ArrayCollection();
174
are onetomany and manytoone relationships different?
no they are the same relationship but viewed from different sides
175
why did we use the name of the entire entity as the field name of the relation?
because we are setting the entire object not just the id
176
how to save a relation in the database?
u first create the object then relate it to the relation comment = new comment(); comment->setArticle($article);
177
what is an authentication system?
it's a way for your users to login whether it's a login form , an api authentication
178
how to make a user entity from the console?
php bin/console make:user
179
what does make:user need for it to work?
composer require security
180
should ur app check for a user password?
if it's an api then probably no
181
what is the first step in creating an authentication system?
create a user class
182
what is the one rule u should follow when u create a user class?
it must implement the UserInterface
183
what are the two methods that the userinterface provide?
getUserName and getRoles
184
what is getUserName used for in the user class?
to get the display name for the debug toolbar that's all
185
what is getRoles used for in the user class?
user permissions
186
what does each user class have in security.yaml?
a user provider
187
what is the type of the roles property in the user class?
it's a json column and an array in phpbut mysql 5.6 or lower doesn't have native json column type in mysql to it will use json_encode
188
what yaml file contains the server version?
doctrine.yaml
189
what is the job of the user provider?
it reloads user data from session and some additional jobs like remember me or impersonation which is to switch between users
190
do u need to configure a user provider?
if ur user class is an entity u don't need to do that but if the user class is just a normal class then make:user will make a user provider for u as well
191
how does the user provider reload the user from session?
At the end of every request (unless your firewall is stateless), your User object is serialized to the session. At the beginning of the next request, it's deserialized and then passed to your user provider to "refresh" it (e.g. Doctrine queries for a fresh user). Then, the two User objects (the original from the session and the refreshed User object) are "compared" to see if they are "equal". By default, the core AbstractToken class compares the return values of the getPassword(), getSalt() and getUsername() methods. If any of these are different, your user will be logged out. This is a security measure to make sure that malicious users can be de-authenticated if core user data changes. However, in some cases, this process can cause unexpected authentication problems. If you're having problems authenticating, it could be that you are authenticating successfully, but you immediately lose authentication after the first redirect. In that case, review the serialization logic (e.g. SerializableInterface) if you have any, to make sure that all the fields necessary are serialized.
192
what is a firewall?
it's the authentication system
193
how many firewalls can u have per request?
only one firewall can be active per request
194
how does a firewall match your request?
by using the pattern: key in security.yaml u can use host , path , HTTP methods or a service and u can leave the pattern: key and don't write it to make the firewall match all requests
195
what is the main firewall?
it's the firewall that handles all the requests and doesn't have a pattern key
196
does not having a pattern: key in the firewall mean that we have no authentication?
no it just means it will match to any url but the anonymous : lazy key means that u can be authenticated as an anonymous user
197
why does authentication in symfony feel a bit like magic?
because instead of building a route and a controller to handle login u will activate an authentication provider
198
what is an authentication provider?
some code that runs automatically before your controller is called
199
is it better to use the build in authentication provider or make a guard authenticator?
guard authenticator?
200
what is a guard authenticator?
a class that allows you to control every part of the authentication proccess
201
what is the guard authenticator used for?
login forms or api authentication
202
how to make a registeration form in the bundle?
php bin/console make:registeration-form
203
how to create a login form?
php bin/console make:auth
204
what does make:auth for loginform create?
1- route and a controller 2- template to render the login form 3- a guard authenticator class that processes the login 4- updates to the main security file
205
what are the steps u should do after u make a login form?
u allow access in securty.yaml to be accesses anonymously - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY } add your authenticator under guard: authenticators: - App\Security\LoginFormAuthenticator