Symfony 2

From the Ground Up







By:
Aaron Scherer

Who is this guy?


Symfony 2

  • Full Version Released in July 2011
  • PHP Model-View-Controller Framework
  • Heavily Component Based
  • Semi Event Driven


Projects they back / Are part of
  • Composer
  • Twig
  • Doctrine
  • Silex
  • Laravel




When should you use it?

HELLO WORLD



Just Kidding.

Due to the size of the Symfony2 Framework, a simple "Hello World" application doesn't make a whole lot of sense.

The same thing can be said for a static, or personal website (for the most part).

ENTERPRISE APPLICATIONS

Here is where Symfony2 really shines.


  • Unlimited design possibilities
  • Powerful Caching Capabilities
  • Tons of Third Party Bundles/Addons
  • Lots of Support

Our first app



A Todo App

You can follow along, and grab a copy of the finished result at
https://github.com/aequasi/symfony-presentation 

Starting off


Lets get the base code set up



This will download all the code for symfony, and all the third party dependencies. It will also ask you for some info for the parameters.

Getting rid of the junk


Now, lets clean stuff up. 


then open 'app/AppKernel.php' up in your favorite editor, and delete this line (should be line 22)




Creating a Bundle


Now we are ready to start adding our code.

SensioLabs has a cool bundle that introduces a few commands that allows us to run a simple command to create our TodoBundle really quickly


This will create all of the base code for us to start, and register the bundle in the AppKernel

CREATING A BUNDLE - Cont.d


Most of the stuff we are going to be doing is in Controller and Resources.  I'm not going to cover Dependency Injection or Tests here.

Right off the bat, delete 'Controller/DefaultController.php' and 'Resources/views/Default'

Database Stuff

Lets start making our database schema. Symfony2 Partners with Doctrine for a moderately simple, but extremely powerful database interaction.

First off, lets start by making our Task entity (or model).



This will create the file: 'src/SDPHP/TodoBundle/Entity/Task.php' and  'src/SDPHP/TodoBundle/Entity/TaskRepository.php'

Database Stuff - Cont.d



From here, we can actually build our database.



Theres also a command to drop the current schema, and a command to update the current schema when you update your entities.

Interacting with the Tasks

Symfony2 makes it pretty simple to set up a CRUD controller and views



This will make a 'TaskController.php' inside Controllers, a couple views inside 'Resources/views', a Form inside 'Form/' and a test for your Controller.

This controller contains all the CRUD methods for interacting with the Tasks in the database.

Lets make a user interface - CONT.d

Base Template ( Everything should extend this )
Open up 'app/Resources/views/base.html.twig'
All of the Task views already extend this

LETS MAKE A USER INTERFACE - CONT.D

With the javascript and css files in the 'Resources/public' directory, we can now run:

which creates relative symlinks from the bundle to the web directory. Viewing the project will now show you:

LETS MAKE A USER INTERFACE - CONT.D

With a little bit of tweaking of the templates, I've removed the 'ID' field and the 'Actions', and changed  'Status' to 'Completed'



I'm also going to do a little tweaking in the "Create a new entry" page and remove 'Status' from the form.

LETS MAKE A USER INTERFACE - CONT.D

I'm also going to do a little tweaking in the "Create a new entry" page and remove 'Status' from the form.

Open up 'Form/TaskType.php' and comment out the line that adds 'status' to the form.


LETS MAKE A USER INTERFACE - CONT.D

Before that will work though, we will need to add a default value to the 'status' property of the 'Task' entity.

In 'Entity/Task.php', fix the variable like this:

In the 'Controller/TaskController.php', change the 'createAction' function to redirect you to the 'indexAction'

LETS MAKE A USER INTERFACE - CONT.D

You can now create a task

Now, lets add some Javascript and tweak the Controller a bit, so when you click on a row, it toggles the 'Completed' status.

If you cloned my repo, the Javascript/CSS will already be there.

LETS MAKE A USER INTERFACE - CONT.D

In the controller, lets make a new function, 'toggleStatusAction', that accepts a task id.

Now, we can call this action with an ajax call.

LETS MAKE A USER INTERFACE - CONT.D


Now, we can just click on a row, and it will toggle the status.


And thats a wrap




Theres a lot more to Symfony2!



Ask me things!
Made with Slides.com