A Symfony Request in Action¶
Without diving into too much detail, here is this process in action. Suppose you want to add a/contact
page to your Symfony application. First, start
by adding an entry for /contact
to your routing configuration file:
This example uses YAML to define the routing
configuration. Routing configuration can also be written in other formats
such as XML or PHP.
When someone visits the /contact
page, this route is matched, and the
specified controller is executed. As you'll learn in the routing chapter,
the AcmeDemoBundle:Main:contact
string is a short syntax that points to a
specific PHP method contactAction
inside a class called MainController
:1 2 3 4 5 6 7 8 9 10 11 12 | // src/Acme/DemoBundle/Controller/MainController.php
namespace Acme\DemoBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class MainController
{
public function contactAction()
{
return new Response('<h1>Contact us!</h1>');
}
}
|
Response
object with the HTML
<h1>Contact us!</h1>
. In the controller chapter,
you'll learn how a controller can render templates, allowing your "presentation"
code (i.e. anything that actually writes out HTML) to live in a separate
template file. This frees up the controller to worry only about the hard
stuff: interacting with the database, handling submitted data, or sending
email messages.Symfony2: Build your App, not your Tools.¶
You now know that the goal of any app is to interpret each incoming request and create an appropriate response. As an application grows, it becomes more difficult to keep your code organized and maintainable. Invariably, the same complex tasks keep coming up over and over again: persisting things to the database, rendering and reusing templates, handling form submissions, sending emails, validating user input and handling security.The good news is that none of these problems is unique. Symfony provides a framework full of tools that allow you to build your application, not your tools. With Symfony2, nothing is imposed on you: you're free to use the full Symfony framework, or just one piece of Symfony all by itself.
Standalone Tools: The Symfony2 Components¶
So what is Symfony2? First, Symfony2 is a collection of over twenty independent libraries that can be used inside any PHP project. These libraries, called the Symfony2 Components, contain something useful for almost any situation, regardless of how your project is developed. To name a few:- HttpFoundation - Contains
the
Request
andResponse
classes, as well as other classes for handling sessions and file uploads; - Routing - Powerful and fast routing system that
allows you to map a specific URI (e.g.
/contact
) to some information about how that request should be handled (e.g. execute thecontactAction()
method); - Form - A full-featured and flexible framework for creating forms and handling form submissions;
- Validator - A system for creating rules about data and then validating whether or not user-submitted data follows those rules;
- ClassLoader - An autoloading library that allows
PHP classes to be used without needing to manually
require
the files containing those classes; - Templating - A toolkit for rendering templates, handling template inheritance (i.e. a template is decorated with a layout) and performing other common template tasks;
- Security - A powerful library for handling all types of security inside an application;
- Translation A framework for translating strings in your application.
The Full Solution: The Symfony2 Framework¶
So then, what is the Symfony2 Framework? The Symfony2 Framework is a PHP library that accomplishes two distinct tasks:- Provides a selection of components (i.e. the Symfony2 Components) and third-party libraries (e.g. Swift Mailer for sending emails);
- Provides sensible configuration and a "glue" library that ties all of these pieces together.
Symfony2 provides a powerful set of tools for rapidly developing web applications without imposing on your application. Normal users can quickly start development by using a Symfony2 distribution, which provides a project skeleton with sensible defaults. For more advanced users, the sky is the limit.
No comments:
Post a Comment