21.10.2015 Views

1-33

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Symfony2 – Franz Jordán 2011<br />

Without diving into too much detail, let's see this process in action. Suppose you want to add<br />

a/contact page to your Symfony application. First, start by adding an entry for /contact to<br />

your routing configuration file:<br />

contact:<br />

pattern: /contact<br />

defaults: { _controller: AcmeDemoBundle:Main:contact }<br />

This example uses YAML to define the routing configuration. Routing configuration can also be<br />

written in other formats such as XML or PHP.<br />

When someone visits the /contact page, this route is matched, and the specified controller is<br />

executed. As you'll learn in the routing chapter, the AcmeDemoBundle:Main:contact string<br />

is a short syntax that points to a specific PHP method contactAction inside a class<br />

called MainController:<br />

class MainController<br />

{<br />

public function contactAction()<br />

{<br />

return new Response('Contact us!');<br />

}<br />

}<br />

In this very simple example, the controller simply creates a Response object with the HTML<br />

"Contact us!". In the controller chapter, you'll learn how a controller can render<br />

templates, allowing your "presentation" code (i.e. anything that actually writes out HTML) to live<br />

in a separate template file. This frees up the controller to worry only about the hard stuff:<br />

interacting with the database, handling submitted data, or sending email messages.<br />

Symfony2: Build your App, not your Tools.<br />

You now know that the goal of any app is to interpret each incoming request and create an<br />

appropriate response. As an application grows, it becomes more difficult to keep your code<br />

organized and maintainable. Invariably, the same complex tasks keep coming up over and over<br />

again: persisting things to the database, rendering and reusing templates, handling form<br />

submissions, sending emails, validating user input and handling security.<br />

The good news is that none of these problems is unique. Symfony provides a framework full of<br />

tools that allow you to build your application, not your tools. With Symfony2, nothing is imposed<br />

on you: you're free to use the full Symfony framework, or just one piece of Symfony all by itself.<br />

Standalone Tools: The Symfony2 Components<br />

So what is Symfony2? First, Symfony2 is a collection of over twenty independent libraries that<br />

can be used inside any PHP project. These libraries, called the Symfony2 Components, contain<br />

9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!