Friday, August 30, 2013

How to configure Symfony2

I have started to develop my web application in symfony in last November, I have faced lots of issues with configuring Symfony framework, Actually it is essay to setup and configure. See the following deals about Symfony. The goal of this blog is to setup and configure the symfony application.

What is Symfony? 
Symfony  is PHP based web application development framework. That having power to scall your big application with security.

Download - Download symfony from http://symfony.com/download

Directory Structure OF Symfony2
Symfony2.3 durectory structure include following six directories,
path/to/webroot/ <- your web server directory (sometimes named htdocs or public)
    Symfony/ <- the new directory
        app/
            cache/
            config/
            logs/
        src/
            ...
        vendor/
            ...
        web/
            app.php
            ...

app - App directory contain AppKernel.php . This class is used to register the bundle. For registering the bundle,  just need to create object of bundle class.

Als app directory contain cache, config, logs, and also resourses directory. Cache directory creates a framework readable code as per dev/prod environment. Logs directory contain log file. Symfony framewor logs each event, info, or exception in log file. Config directory contain all application configuration. And Resources directory contain all global resources like base twig or base layout, Some time we can add a common macro.

src - This directory contain the actual development classes. In symfony2.3 download backup contain default "Demo bundle". As per requirement user can define single bundle application or multiple bundle for as per functionality or requirement.

vendor - This directory contain the all necessary library for the project, We can call this as bundles. Symfony basic bundle provides the lots of bundles, like Swift Mailer, Doctrine, Form, Twig etc.

User can also configure custom bundles, Actually that is the power of symfony to use third part bundles as a components.

web- This directory contain all public resources. app.php is the start up file.

After coping Symfony into your web directory, start apache and run following URL

http://localhost/app_dev.php/

 After running this URL, Sye mfony welcome page will show as the result of your hardwork.

Symfony Welcome Page
First time, We need to configure the PHP extension and configuration. Click on configuration button on welcome screen to check required extension and packages. Configuration page will automatically check s and give you report what is require.

If any missing extension then install that extension first.

If you already setup all extension and dependency then click on  Run the Demo button.

How to add my first page?
The basic componenets for creating your first hello page is,

Controller - Controller is class, in that we can define all action in framework, Any userdefine controller class must be extends from "Symfony\Bundle\FrameworkBundle\Controller\Controller" class.

To create your own controller class, Click on controller directory and create new class named HelloController and extend from Symfony Controller class.

Action - Each action function contain postfix Action. e.g, HelloAction

class HelloController extends Controller { 

    public function helloAction(Request $request) {
         new $this->render('DemoBundle:hello:hello.html.twig', array());;
    }
}

Route - To execute the logic from web we need to create route.  See following sample route,

_hello:
    pattern:  /hello
    defaults: { _controller: DemoBundle:Hello:hello }

pattern - Pattern conten the actual URL the will append after app.php or apps_dev.php.
defaults - Its path of action,

Twig -To design view, Symfony used twig as a default template engine, But We can setup smarty also with symfony by changing default template engine. add hello.twig in "Resources->views->hello" directory.

Add Hello message in twig page. Save project and run following url.

http://localhost/app_dev.php/hello

Your Hello page will be display.

If you want to learn more about Symfony then go to http://symfony.com for more information. Enjoy this steps and run your first Symfony2.3 application.

Total Pageviews