+ All Categories
Home > Technology > Symfony2

Symfony2

Date post: 16-Aug-2015
Category:
Upload: nursultan-turdaliev
View: 212 times
Download: 4 times
Share this document with a friend
Popular Tags:
39
Symfony2 PHP Framework Turdaliev Nursultan [email protected]
Transcript
Page 1: Symfony2

Symfony2 PHP Framework

Turdaliev [email protected]

Page 2: Symfony2

Symfony at a Glance

• Symfony is a PHP framework.• Symfony is a framework, a set of tools

and a development methodology.

Page 3: Symfony2

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

Page 4: Symfony2

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

Page 5: Symfony2

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.

Page 6: Symfony2

Why should I use a framework?To make long story short.

A framework is not an absolute necessity,

but nonetheless it is very useful

A framework is a pledge of quality, upgradability and maintainability of applications at lower cost.

Applications developed using a framework are

interoperable with market standards.

Page 7: Symfony2

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.

Page 8: Symfony2

When should I use a framework?To make long story short.

A framework can be tailored to all

requirements.

..but that is not always necessary, especially when the requirements are of a more generic nature.

Before making your selection, map out your

current and future requirements in detail, and

then compare the various solutions available

on the market.

Page 9: Symfony2

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. Reputation2. Permanence3. References 4. Innovation5. Resources

http://symfony.com/six-good-reasons

Page 10: Symfony2

6 good reasons to use SymfonyTo make long story short.

You can’t go wrong by choosing Symfony!

And no one has ever been fired for using

Symfony.

Page 11: Symfony2

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:

Page 12: Symfony2

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.

Page 13: Symfony2

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.

Page 14: Symfony2

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.

Page 15: Symfony2

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)?

Page 16: Symfony2

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)

Page 17: Symfony2

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).

Page 18: Symfony2

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.

Page 19: Symfony2

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.

Page 20: Symfony2

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.

Page 21: Symfony2

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..

Page 22: Symfony2

To make long story short.

Choosing a framework must not be taken

lightly; it is a long-term commitment.

Make sure that you make the right

selection!

Page 23: Symfony2

Ready?

Page 24: Symfony2

Symfony2 and HTTP Fundamentals

Page 25: Symfony2

HTTP is SimpleHTTP (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.

Page 26: Symfony2

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.

Page 27: Symfony2

In HTTP-speak, this HTTP request would actually look something like this:

GET / HTTP/1.1Host: 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.

Page 28: Symfony2

    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

Page 29: Symfony2

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.19Content-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.

Page 30: Symfony2

        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.

Symfony is architected

to match this reality.

Requests, Responses and Web Development

Page 31: Symfony2

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:

Page 32: Symfony2

    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

Page 33: Symfony2

    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

Page 34: Symfony2

    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

Page 35: Symfony2

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.

Page 36: Symfony2

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.

Page 37: Symfony2

The Front Controller To make long story short!

Each request executes a

front controller file;

The routing system determines which PHP function should be executed based on information from the request and routing configuration you've created;

The correct PHP function is executed, where

your code creates and returns the appropriate

Response object.

Page 38: Symfony2

Installing and Configuring

Symfony

Page 39: Symfony2

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


Recommended