Sunday, January 6, 2013

Getting started with Symfony2 - Part 1

After installing Symfony 2.1 and going through the documentations, everything seems simple enough. To start developing an application from scratch, first create a project, next create a Bundle and start adding codes.

Several assumptions;
  1. Installation was done as in my previous posting, including creating the project called mysymfony.
  2. The Bundle is called HelloBundle in Sample. All Bundle names must end with the word Bundle.
  3. All Bundles will be under the vendor named "Sample".
Here are steps to create and delete a Bundle.

A. Create a Bundle
Step 1: Generate the Bundle

cd mysymfony
php app/console generate:bundle --namespace=Sample/HelloBundle --format=yml

Respond to the wizard's questions;
  1. [Enter]
  2. HelloBundle
  3. [Enter]
  4. [Enter]
  5. [Enter]
  6. [Enter]
  7. [Enter]
  8. [Enter]

Step 2: Create the routing.
Still in the symfony directory, edit the file src/Sample/HelloBundle/Resources/config/routing.yml

hello:
    pattern:  /hello/{name}
    defaults: { _controller: SampleHelloBundle:Hello:index }

Step 3: Create the controller file.

vi src/Sample/HelloBundle/Controller/HelloController.php



namespace Sample\HelloBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HelloController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('SampleHelloBundle:Hello:index.html.twig', array('name' => $name));
    }
}

Step 4: Create a view template

vi src/Sample/HelloBundle/Resources/views/Hello/index.html.twig

{% extends '::base.html.twig' %}
{% block body %}
Hello {{ name }}!
{% endblock %}

Step 5: Access the final application

In a web browser, enter the location or URL as

http://localhost/mysymfony/web/app.php/hello/malaysia

---

B. Delete a Bundle
Step 1: Delete the Bundle directory
rm -rf src/Sample/HelloBundle/

Step 2: Remove Bundle routing in /app/config/routing.yml
Example, delete

sample_hello:
    resource: "@SampleHelloBundle/Resources/config/routing.yml"
    prefix:   /

Step 3: Remove Bundle from app/AppKernel.php
Example, delete

new Sample\HelloBundle\SampleHelloBundle(),

Step 4: Clear Symfony cache
php app/console cache:clear 

or

php app/console cache:clear --env=prod --no-debug


Note to self: No promises when Part 2 will be published.

Saturday, January 5, 2013

Installing Symfony2 on Centos 6

Symfony2 framework looks like it provide many useful development tools.

All following codes require access to internet to download the respective files.

Pre installation check list for Symfony2.1.

  1. Installed and running Apache httpd
  2. Installed php 5.3 or higher
  3. Installed GIT 1.7 or higher
Install PHP
Lots of resources available online.

Ensure additional php packages are installed, including
  • php-cli
  • php-intl
  • php-mysql (if you use MySQL database)
  • php-gd
  • php-pecl-apc
  • php-mbstring

Check that /etc/php/ini timezone is edited.
Example;
date.timezone = "Asia/Kuala_Lumpur"

Install GIT
sudo yum install git-core

Install Composer for PHP
cd /var/www/html
curl -s https://getcomposer.org/installer | php


Install Symfony2.1
Step 1: Download from http://symfony.com/download and extract


tar xvzf Symfony_Standard_Vendors_2.1.6.tgz

Read the text file Symfony/README.md

cp composer.phar Symfony

Step 2: Basic configuration



Step 3: Create your first Symfony2 project

cd /var/www/html/Symfony
php composer.phar create-project symfony/framework-standard-edition /var/www/html/mysymfony 2.1.6

Where
  • create-project is the command to create a project with 3 arguments
  • symfony/framework-standard-edition is the framework to be installed
  • /var/www/html/mysymfony is the new directory or project name to be created and installed with base project files
  • 2.1.6 is the version
Use a web browser to check installed project at

http://localhost/mysymfony/web/config.php

If there is an error, please fix before continuing. I received 2 errors to fix the directory permission for app/cache and app/logs

chown -Rf apache.apache app/logs app/cache


Note: I found following discussion useful

http://forum.symfony-project.org/viewtopic.php?f=32&t=45892

What you should not do for your online portal

At any time if you have launched a portal accessed publicly, its a bad idea to have links showing Minta Maaf.

Just found this on the Malaysian Road Transport Department Portal on  5 Jan 2013.

If its not ready, no need to put it online, right? Just my Opinion.




The link kept directing to http://www.jpj.gov.my/mintamaaf for most of the options I picked.





Blog Archive