Posted on 15-03-05 by Peter

The first blogpost on how to create a symfony site

Awhile back I decided I wanted to learn how to work with the framework Symfony 2. After done some reading and testing I realised that it would be more inspiring for me to actually have a real project so I decided that it was time for me to update my company site where you are now reading this. But I hadn't decided what to put on it and then I got this idea to simply blog about how to build a symfony site from scratch as newbie on symfony framework.

First it's important to know this; it's a plain and simple page, I think this site at this point is the first time in over ten years time that I don't use a database in the background, hence there are i.e no adminpage for posting anything.

To begin with I downloaded the latest symfony book and following the instructions in their to install symfony 2.6. In my case that meant using composer to create my project. Make sure you install the demo, easy to remove later.

                       1
                    
composer create-project symfony/framework-standard-edition symfony
                    

Next I used the command to generate a bundle and with 2.6 this is really easy, it helps you create everything that is needed. In the book the example of creating a bundle is done with namespace and bundlenamne, that is not necessary, you can create a bundlename directly as --namespace=DemoBundle. I tried both ways and decided that I don't know where I'll end up so I created a namespace Letitrock with for start one bundle.

                       1
                    
php app/console generate:bundle --namespace=Letitrock/BlogBundle --format=yml
                    

I'm not going to inform you about serverenvironment and filepermissions etc simply cause that is so much depending on which your environment is but the book is good on that.

Since I didn't want either Appbundle or demobundle in this project I removed those two by deleting the source code but also from the routing files. In my app/config/routing*.yml I made sure that the root path is the resource pointed to my project routing file. these two looks like this:

                       1 2 3 4 5 6 7 8
                    
 //app/routing.yml 
   letitrock_blog:
       resource: "@LetitrockBlogBundle/Resources/config/routing.yml"
       prefix:   /

    //my project routing.yml
    _welcome:
        path:     /
        defaults: { _controller: LetitrockBlogBundle:Default:index }
                    

Ok so the code above in my routing file tells use that there should be a controller named Default and a function called index. Mine is very simple; I'm using a twigfile for rendering and sends back the result.

                       1
                    
 //DefaultController/índexAction 
   return $this->render('LetitrockBlogBundle:Default:index.html.twig');
                    

The twigfile is found in in my project Resource/views/Default/ and twig is to much to write about here but having a base twig file that I extends and then a block body where this is written (remember no db yet..).

So that is the basic of how I set up this site but there are details I've not gone into and that is how I set up the project within a git repository but I also decided to use the Bootstrap theming and sass (and yes there are a bundle for that) but at the moment I've not used the gridsystem.

Next programming step must be to add a secured page for administration of the site like posting here instead of writing in the indexfile and hence I'll need a database. Obviously when a database is implemented the presentation of the content of this site will be easier and better. The "codeblock" used is hardcoded within this template; I'm guessing there are smarter ways to do that then copy and paste, probably possible to do inclusion of the block somehow.