Sajjuks Harry Porter Store

Thursday 9 January 2014

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:
  • YAML
    1
    2
    3
    4
    # app/config/routing.yml
    contact:
        path:     /contact
        defaults: { _controller: AcmeDemoBundle:Main:contact }
    
  • XML
  • PHP
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>');
    }
}
In this very simple example, the controller simply creates a 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 and Response 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 the contactAction() 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.
Each and every one of these components is decoupled and can be used in any PHP project, regardless of whether or not you use the Symfony2 fram

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:
  1. Provides a selection of components (i.e. the Symfony2 Components) and third-party libraries (e.g. Swift Mailer for sending emails);
  2. Provides sensible configuration and a "glue" library that ties all of these pieces together.
The goal of the framework is to integrate many independent tools in order to provide a consistent experience for the developer. Even the framework itself is a Symfony2 bundle (i.e. a plugin) that can be configured or replaced entirely.
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.
ework. Every part is made to be used if needed and replaced when necessary.



No comments:

Post a Comment