This document discusses Symfony, a PHP framework. It provides an overview of Symfony, explaining that it is a PHP framework and a set of tools and development methodology. It then discusses some key aspects of Symfony, including how it provides a toolbox of prefabricated components to write less code more productively, and a methodology for structured application development. The document also summarizes how Symfony handles HTTP requests and responses through its Request and Response classes to provide an object-oriented interface for building web applications. Finally, it briefly outlines how to install Symfony distributions and configure a Symfony project.
2. Symfony at a Glance
• Symfony is a PHP framework.
• Symfony is a framework, a set of tools
and a development methodology.
3. A PHP framework
Basically, a framework consists of:
– A toolbox - a set of prefabricated, rapidly integratable software components. This
means that you will have to write less code, with less risk of error. This also
means greater productivity and the ability to devote more time to doing those
things which provide greater added value, such as managing guiding
principles, side effects, etc.
– A methodology – an “assembly diagram” for applications. A structured
approach may seem constraining at first. But in reality it allows developers to
work both efficiently and effectively on the most complex aspects of a task, and
the use of Best Practices guarantees the stability, maintainability and
upgradeability of the applications you develop
4. Symfony in 5 minutes
Why should I use a framework?
When should I use a framework?
6 good reasons to use Symfony
The technological benefits of Symfony in 6 easy lessons
10 criteria for choosing the correct framework
5. Why should I use a framework?
A framework is not absolutely necessary: it is “just” one of the tools
that is available to help you develop better and faster!
Better, because a framework provides you with the certainty that you are
developing an application that is in full compliance with the business rules,
that is structured, and that is both maintainable and upgradable.
Faster, because it allows developers to save time by re-using generic modules
in order to focus on other areas. Without, however, ever being tied to the
framework itself.
6. Why should I use a framework?
To make long story short.
A framework is a pledge of quality, upgradability
and maintainability of applications at lower cost.
7. When should I use a framework?
• Well... Whenever you need to, while developing a web application or a site, of
course!
• Why would you have a custom piece of furniture made if you could find one
that perfectly matches your décor, right off the shelf? The same is true for
computer applications: Before you dive in to create a framework, a little survey
work is in order.
• Besides a framework, there are other solutions that are available to develop
both Web sites and Web applications: CMSs (Content Management System)
and their supplemental modules, as well as packaged professional solutions
(CRM, e-commerce solutions, etc.). If their native features line up perfectly
with your immediate and future needs, no question about it: Go for it! There is
no point in reinventing the wheel.
• On the other hand, when there are specific needs, specific business rules or
even the need to mix building blocks (content and e-commerce, for
example), a framework solution is necessary: it allows an application to be
developed that perfectly matches your current needs, while still being
innovative.
8. When should I use a framework?
To make long story short.
..but that is not always necessary, especially when
the requirements are of a more generic nature.
9. 6 good reasons to use Symfony
Using a Framework is a good thing. Making the right
choice is even better. Without going too deep into things and in
no particular order, here are 6 good reasons to use Symfony, from
a strategic point of view
1. Reputation
2. Permanence
3. References
4. Innovation
5. Resources
http://symfony.com/six-good-reasons
11. 10 criteria for choosing the correct
framework
You're making progress and that's a good
thing! You already know that you are going to
use a framework to develop your site or your
application. But which one? Here is a checklist
that you can use to avoid making a mistake:
12. Popularity and community size
The more well-known and recognized
the framework is, the more it will be
“living,” evolving and complete: new
ideas, the number and quality of plug-
ins, etc.
13. Philosophy
This is the very essence of the
framework: it is a fundamental criterion for
ensuring that it will meet your needs. A tool
developed by professionals for their own
needs will obviously meet the demands of
other professionals.
14. Sustainability
Before choosing a framework, make
sure that it will be able to keep up with you
for the duration. This simplifies both the
maintenance and upgrading of your
applications.
15. Support
Another criterion that should not be
overlooked is the ease of finding answers to
your questions and getting help. Identify the
support that is available: from the publisher.
From a community (mailing lists, IRC, etc.)?
From Service Companies
(development, support, training)?
16. Technique
To avoid becoming trapped in a
labyrinth, it is always preferable to choose
an interoperable solution; one that
respects best practices in terms of
development (design patterns)
17. Security
Any application is potentially
vulnerable. To minimize risk, it is always
better to select a framework capable of
ensuring security functions (XSS
management, for example).
18. Documentation
It is an absolute necessity to
evaluating the nature, volume and quality
of existing literature about a framework: a
well-documented tool is both easier to use
and more upgradeable.
19. License
Licenses are important simply because
they can have a significant impact on your
applications. For example, an application
developed using a GPL-licensed framework
will necessarily be subject to GPL. On the
other hand, this is not the case for an MIT-
licensed framework.
20. Availability of resources on the
market
Perhaps you would want to have a
technical team surround you during the
development phase or in the longer term, for
both maintenance and upgrades. In other
words, make sure that the skills required for
the tool that you are using are available on the
open market.
21. Try it out!
That's the key! Don't be satisfied with
reading reviews, comments and rumors, good
or bad, on the Internet. By testing it out, you
will be able to make up your own mind and
ensure that you are completely comfortable
with the tool..
25. HTTP is Simple
HTTP (Hypertext Transfer Protocol to the geeks) is a text language that allows two
machines to communicate with each other. That's it! For example, when checking for
the latest xkcd comic, the following (approximate) conversation takes place:
Symfony2 is built from the
ground-up around that
reality. Whether you realize
it or not, HTTP is
something you use
everyday. With
Symfony2, you'll learn how
to master it.
26. Step1: The Client sends a Request
Every conversation on the web starts with a request. The request is a text message
created by a client (e.g. a browser, an iPhone app, etc) in a special format known
as HTTP. The client sends that request to a server, and then waits for the
response.
27. In HTTP-speak, this HTTP request would actually look something like this:
GET / HTTP/1.1
Host: xkcd.com Accept:text/html
User-Agent: Mozilla/5.0
(Macintosh)
Step1: The Client sends a Request
This simple message communicates everything necessary about exactly
which resource the client is requesting. The first line of an HTTP request is
the most important and contains two things: the URI and the HTTP method.
The URI (e.g. /, /contact, etc) is the unique address or location that
identifies the resource the client wants. The HTTP method (e.g. GET) defines
what you want to do with the resource.
28. Once a server has received the request, it knows exactly which resource the client
needs (via the URI) and what the client wants to do with that resource (via the method).
For example, in the case of a GET request, the server prepares the resource and returns it
in an HTTP response. Consider the response from the xkcd web server:
Step 2: The Server returns a Response
29. Translated into HTTP, the response sent back to the browser will look
something like this:
HTTP/1.1200 OK
Date: Sat, 02 Apr 2011 21:05:05 GMT Server : lighttpd/1.4.19
Content-Type: text/html
<html> <!-- ... HTML for the xkcd comic --> </html>
Step 2: The Server returns a Response
The HTTP response contains the requested resource (the HTML content in this case), as well as
other information about the response. The first line is especially important and contains the HTTP
response status code (200 in this case). The status code communicates the overall outcome of the
request back to the client. Was the request successful? Was there an error? Different status codes
exist that indicate success, an error, or that the client needs to do something (e.g. redirect to
another page). A full list can be found on Wikipedia's List of HTTP status codes article.
30. This request-response conversation is the fundamental process that drives all
communication on the web. And as important and powerful as this process is, it's
inescapably simple.
The most important fact is this: regardless of the language you use, the type of
application you build (web, mobile, JSON API), or the development philosophy you
follow, the end goal of an application is always to understand each request and
create and return the appropriate response.
Requests, Responses
and Web Development
31. Requests and Responses in PHP
So how do you interact with the "request" and create a "response" when using PHP?
In reality, PHP abstracts you a bit from the whole process:
32. Symfony provides an alternative to the raw PHP approach via two classes that allow you
to interact with the HTTP request and response in an easier way. The Request class is a
simple object-oriented representation of the HTTP request message. With it, you have all
the request information at your fingertips:
Requests and Responses in Symfony
33. Symfony also provides a Response class: a simple PHP representation of an HTTP
response message. This allows your application to use an object-oriented interface to
construct the response that needs to be returned to the client:
Requests and Responses in Symfony
34. Like HTTP itself, the Request and Response objects are pretty simple.
The hard part of building an application is writing what comes in between.
In other words, the real work comes in writing the code that interprets the
request information and creates the response.
Your application probably does many things, like sending emails,
handling form submissions, saving things to a database, rendering HTML
pages and protecting content with security. How can you manage all of this
and still keep your code organized and maintainable?
Symfony was created to solve these problems so that you don't have to.
The Journey from the Request to the Response
35. Traditionally, applications were built so that each "page" of a site was its own physical
file:
The Front Controller
There are several problems with this approach, including the
inflexibility of the URLs (what if you wanted to change blog.php to
news.php without breaking all of your links?) and the fact that each
file must manually include some set of core files so that security,
database connections and the "look" of the site can remain
consistent.
A much better solution is to use a front controller: a single PHP file that handles every
request coming into your application. For example:
Now, every request is handled exactly
the same way. Instead of individual URLs
executing different PHP files, the front
controller is always executed, and the
routing of different URLs to different
parts of your application is done
internally. This solves both problems
with the original approach. Almost all
modern web apps do this - including
apps like WordPress.
36. Stay Organized
Each "page" of your site is defined in a routing configuration file that maps different URLs to different PHP
functions. The job of each PHP function, called a controller, is to use information from the request - along
with many other tools Symfony makes available - to create and return a Response object. In other
words, the controller is where your code goes: it's where you interpret the request and create a response.
39. Installing a Symfony2 Distribution
First, check that you have installed and configured a Web server
(such as Apache) with PHP 5.3.8 or higher. For more information on
Symfony2 requirements, see the requirements reference