Sie sind auf Seite 1von 27

Symfony

Presented by Hayden Pierce

Hayden Pierce
hayden@haydenpierce.com
https://www.linkedin.com/in/hayden-pierce-68896185/
Tonight
1. What is Symfony?
2. What is MVC?
3. Starting with Symfony
4. Making a Contact Page
5. 3 Major Concepts
Routing
Controllers
Services
6. Pros and Cons
What is Symfony?
Symfony Components Symfony Full Stack Web Framework

Collection of small, reusable components: Generally what people mean when they say
Symfony:
Asset ICU
BrowserKit Intl
Cache
ClassLoader
LDAP Bundle system for file organization
Locale

INCLUDED IN
Config Options Resolver Opinionated file structure (by default)
Console Process
CssSelector Property Access Symfony Profiler
Debug Property Info
DependencyInjection Routing
Includes other non-Symfony tools.
Dotenv Security Doesnt require use of all components
DomCrawler Serializer
Event Dispatcher StopWatch
Expression Language Templating
FileSystem Translation
Finder Validator
Form VarDumper
Guard Workflow
HTTPFoundation YAML
HTTPKernel

Presentation will cover Dependcy Injection, HTTPFoundation, and Routing


components (at a high level).
Symfony framework documentation seems to promote a certain type of file
organization, you can fight against this and do whatever if you like.
Symfony Profiler
Shows what Symfony is doing
internally:

Forms
Performance
Events
Routing info
Active Settings

Ultimately not a huge deal.


Helpful for tricky debugging.

I wouldnt recommend spending time learning this when getting your feet wet with
Symfony.
Symfony Ecosystem

Doctrine Twig SwiftMailer

ORM - OOP Templating Engine. Sends emails. (Similar to


Database stuff Uses its own PHPMailer)
non-PHP Syntax.

Twig - Super important.


Doctrine - important for database stuff.
Swiftmailer - could easily be replaced with something else.
What is MVC?
View - Captures user input

Model - performs most business


logic, independent of user actions.

Controller - acts as a bridge


between view and model, parsing
the view into arguments for the
model.

Image credit: wikiepdia: https://commons.wikimedia.org/wiki/File:MVC-basic.svg

MVC is an architectural pattern that promotes the separation of concerns by


organizing applications into three major components: The view, which captures and
interrupts interactions from the user (incoming HTTP requests by visiting links), the
model - which solves problems without regard to the format of the view, and the
controller which parses items from the view and provides the arguments to the model.
What is MVC?
1. Each HTTP request is handled by the
front Controller where it is routed to a Controller
controller action

View

Model

Image credit: symfony.com:


http://symfony.com/legacy/doc/gentle-introduction/1_4/en/02-Exploring-Symfony-s-Co
de#chapter_02_sub_symfony_s_mvc_implementation
What is MVC?
1. Each HTTP request is handled by the
front Controller where it is routed to a Controller
controller action
2. Actions parse the request
View

Model

Image credit: symfony.com:


http://symfony.com/legacy/doc/gentle-introduction/1_4/en/02-Exploring-Symfony-s-Co
de#chapter_02_sub_symfony_s_mvc_implementation
What is MVC?
1. Each HTTP request is handled by the
front Controller where it is routed to a Controller
controller action
2. Actions parse the request
3. Perform business logic in the Model View

Model

Image credit: symfony.com:


http://symfony.com/legacy/doc/gentle-introduction/1_4/en/02-Exploring-Symfony-s-Co
de#chapter_02_sub_symfony_s_mvc_implementation
What is MVC?
1. Each HTTP request is handled by the
front Controller where it is routed to a Controller
controller action
2. Actions parse the request
3. Perform business logic in the Model View

4. provide a response with the View

Model

Image credit: symfony.com:


http://symfony.com/legacy/doc/gentle-introduction/1_4/en/02-Exploring-Symfony-s-Co
de#chapter_02_sub_symfony_s_mvc_implementation
Lightning Example App
Goals:

Show workflow from installation to completion


Examine M, V and C parts of a Symfony Framework app.

Result:

Create a single contact form that sends an email.


2 routes (Controller), 2 actions (Controller), 1 service (Model),
2 templates (View)

https://github.com/hpierce1102/FVCPSymfony
Up and Running with Symfony
Symfony CLI Installer:

$ symfony new myProject

Builds scaffolding
Latest version of the framework
Latest structure recommendations

http://symfony.com/doc/current/setup.html
Setup URL -
Contact Form
Add route to routing.yml.

Routes include:

The URL
A pointer to a controller action
Controller Action -
Contact Form
Handle the specific HTTP request:

Controllers parse the request and return


a response.
Send back HTML -
Contact Form
Arrange HTML is a useful way:

Templates can hide or display certain


elements (but not here).
$ php bin/console server:start
$ php bin/console server:start
Setup URL -
Submission
Routes include:
The URL
A pointer to a controller action
A required HTTP method (POST
vs. GET)
Controller Action -
Submission
Handle the specific HTTP request.

Controllers parse the request:

Get the name and message pass it to a


service.
Create a Service -
Mailer Service
Services are reusable components:

Take input from applications globals*


Can take other services as inputs
Register Service -
Mailer Service
Services are registered in the
services.yml configuration to:

Declare the service to Symfony


Tell Symfony how to create it.
Utilize Service -
Submission
Services are retrieved from the service
container:

Control over parameters was


provided to services.yml.
Send back HTML -
Contact Form
Arrange HTML is a useful way:

Templates can hide or display certain elements (but not


here).
$ php bin/console server:start
Components
Routing:

Mapped URLs to internal logic (controller actions)

HTTPFoundation:

Converted the HTTP request into an accessible $request object.

DependencyInjection:

Symfonys mailer class was injected into out contact_mailer service


Wrap up
Pros Cons

Extreme abstraction allows extreme Steep learning curve


flexibility Probably overkill for smaller apps.
Vast number of existing components
Neat organization
Solid developer experience tools

Find the example app on Github: https://github.com/hpierce1102/FVCPSymfony


Symfony
Presented by Hayden Pierce

Hayden Pierce
hayden@haydenpierce.com
https://www.linkedin.com/in/hayden-pierce-68896185/

Das könnte Ihnen auch gefallen