-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymfony4
113 lines (101 loc) · 4.6 KB
/
symfony4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use:
1- symfony web-skeleton for a normal web app.
2- symfony skeleton for micro service, API, or a console app.
====
can use php's internal server for development, but symfony recommends using a better server using:
composer require server
then run it using:
bin/console server:run
the bin/console command is like the laravel artisan command.
====
recommended plugins for symfony in phpstorm:
symfony support( which needs to be enable for each project).
php annotations
php toolbox
====
routes normally resided into config/routes.yaml file, it's something like:
index:
path: /
controller: App\Controller\DefaultController::homaPage
but you can also use annotations to write routes directly inside the controller, above each action method.
to start using annotations, you have to require it:
composer require annotations
then for e.g to make a route:
/**
* @Route("/")
*/
and for a wildcard routes, you can use /{something}, e.g:
/**
* @Route("/articles/{slug}", name="showArticle")
*/
consider that you can match the following with routes:
regex
HTTP methods
host names
====
symfony flex:
is a composer plugin that can do 2 things:
1- aliasing a package, e.g:
composer require sec-checker
this actually requires sensiolabs/security-checker package, but with an alias name, which flex can resolve.
you'll notice a changes to the file: "symfony.lock", this file is managed by flex, keeping track of all recipes that have been installed.
2- manage and run recipes.
recipes can add configuration files, create directories, or even modify some files like .gitignore to straighten the usage of
some package without any further configuring.
for e.g: the recipe included with sec-checker adds some new script to composer.json file,
so that whenever you install a new package, it will run this script to check for package vulnerabilities.
flex can also remove recipes when you un install some package.
====
Twig:
can be imported using:
composer require twig
this will add some bunch of files and directories to your project, e.g:
1- /config/bundles.php
bundles are the plugin system for symfony, and whenever you require one, flex add it here to be used directly.
2- /templates directory
the path of this is totally configured (among other things) from:
3- /config/packages/twig.yaml
====
Twig files are mostly named of the form: 'something.html.twig',
and has to reside in the templates directory.
it has 3 syntax:
1- {{ }}
2- {% %}
3- {# #}
to render a template from a controller, you'll likely extend "AbstractController" from symfony bundle,
it's not needed, but it gives some helper methods, like:
$this->render('path_to_file_discarding_base_templates_dir', [parameters])
====
symfony packs gather some libraries together in a single call, e.g:
composer require debug
this will install the debug-pack package, which includes about 6 other libraries.
debug-pack itself is just a composer.json file, that contains them all.
you can not however control the version of a single one of those, or even remove only a single library,
solution is to unpack the pack, then do whatever needed, e.g:
composer unpack debug
this removed the main debug-pack package, and add it's contents individually, allowing you to control every single aspect.
====
when using Twig along with some JS,CSS or images files, BEWARE that sometimes you'll need to remove the:
var/cache/dev/*
var/cache contains symfony's cached files, including templates.
whenever refer to internal file of those, remember wrapping it inside: asset() function,
which you'll need to:
composer require asset
this installs the symfony asset component.
====
bin/console debug:router
this prints all the app routes.
when using annotations in routes, a name is automatically generated for our routes, you should not depend on this generation
as changing some code parts would change the name generated, breaking all references to this specific route name.
the path('route_name') returns the URL to related route.
====
you typehint something, symfony passes the right instance for you, through auto-wiring mechanism.
where does those service instances come from ?
every service is installed inside the: container, and it has an internal name, e.g: router, logger, twig, kernel, and filesystem.
now bundles( which are symfony's plugin system), they prepare those services and install them inside the container.
inside: config/bundles.php, you can find almost all installed bundles( or plugins ),
the recipes system keeps updating this files everytime you install a new bundle.
RECAP: symfony is a collection of services,
bundles prepare those service objects and put them inside the container.
e.g: monolog bundle provides the logger service.
====