Explore Yourself !!!


Build your first symfony project
January 11, 2009, 9:41 am
Filed under: Symfony | Tags: , , , ,

Good time to build first simple symfony project. The project is very simple one. Build a Signup form. Don’t blame me, how a signup form can be a project !!!. Yes it is :) Lets Start.

Before start follow the instructions make sure you have successfully configured symfony in your local PC. which mean, your PHP, MySql and Apache are perfectly installed. Moreover, PEAR library is also installed and you have enabled PHP CLI (Command Line Instruction). Fair enough :) you have all these installed and support. So lets get start!!!

First create a folder with your project name. for example, for this tutorial we name the project signup. Say we create a folder at d:\dev\signup and we create two sub folder named lib and vendor. which mean the directory structure is exactly d:\dev\signup\lib\vendor. okay !! now donwload symfony latest stable package from here and extract it inside d:\dev\signup\lib\vendor. rename the extract folder to symfony. which mean our structure is like D:\dev\signup\lib\vendor\symfony\ and at this location we get some folders (data, doc, lib, etc etc).

Hang ON!! Don’t dive!! Before that, we need to analyze what exactly we are going to do. we have to build a signup page and a success page. That’s all !!!! Signup Form: only 3 fields: name, email and sex(male or female). as though you all are quite mature lets define specific. we use html text box for name and email and radio button for sex. Success Form: simply a message. Operation Successful !! Very easy requirement for us. if we start write raw coding then it must not take more then 20 minutes of us. Right? Let’s see how long symfony take for this tiny requirement.

LETS DIVE !! DIVE !! DIVE !! DIVE !! DIVE !! DIVE !!

  • Goto Start->Run->cmd press enter
  • navigate to d:\dev\signup
  • Setup your PROJECT by d:\dev\signup>php lib\vendor\symfony\data\bin\symfony generate:project signup =) lot of files automatically generated for you :)
  • Create frontend application using d:\dev\signup>php symfony generate:app –escaping-strategy=on –csrf-secret=Unique$ecret frontend =) lot of file again automatically generated for you at d:\dev\signup\app\frontend :)

Upto this step these are common for any types of projects like facebook.com or ebay.com. :) Lets dive into symfony to build our HUGE signup project. Till now we have not write a single line of code and it’s time for us to do that. Let’s remember our requirement. it was a signup form with only 3 fields. This is the most most most tricky one. Lot’s of class, form, validation etc etc will create automatically based on our object signup. So lets build our object first. How? very easy


// D:\dev\signup\config\schema.yml
propel:
member: id: ~ name: { type: varchar(255), required: true }
email: { type: varchar(255), required: true }
sex: { type: varchar(255), required: true }

and customize your database file

// D:\dev\signup\config\databases.yml
dev:
propel:
param:
classname: DebugPDO
test:
propel:
param:
classname: DebugPDO
param:
classname: DebugPDO
dsn: 'mysql:host=localhost;dbname=signup_test'
username: root
password: null
all:
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
dsn: mysql:dbname=signup_dev;host=localhost
username: root
password:
encoding: utf8
persistent: true
pooling: true

Is the class, form etc etc created by writing only these above line of code? It’s impossible. Yes it’s really impossible until you run few lines of code at command prompt. Lets do that.

  • first create create two database named signup_dev and signup_test from your browser http://localhost/phpmyadmin
  • now from command promt: d:\dev\signup>php symfony propel:build-all automatically generate lot of classes for you inside d:\dev\signup\lib\. don’t get any file? don’t worry look inside model, form, filter folder :D

i hope you have fair experience of PORT BASED SETUP. if No then you are most welcome :) . simply add these lines at the end of your configuration file apache\conf\httpd.conf


Listen 127.0.0.1:8081
DocumentRoot "D:\dev\signup\web" DirectoryIndex index.php
AllowOverride All Allow from All
Alias /sf D:\dev\signup\lib\vendor\symfony\data\web\sf
AllowOverride All Allow from All

Run http://localhost:8081/frontend_dev.php what comes in browser? “Symfony Project Created”. right? which mean’s you are Successful at first stage!! YEAAAAAAA !! Lets celebrate!!

Well let’s move forward.
From command promt: d:\dev\signup>php symfony generate:module frontend member
Have a look inside app\frontend\module. A ready made module named member for us :)
another one: d:\dev\signup>php symfony propel:generate-crud frontend member Member

That’s it!!! Member module is 100 % complete. which means you can add, edit, update, delete a member with proper VALIDATION. did you write a single line of php code? i don’t think so. browse

http://localhost:8081/member
default display a list of members and option for add new one, edit existing one, delete or update any one.

hope you have not enjoyed. because either you know symfony very well, then this article is waste of time for you :D in contrast, no knowledge of symfony means this article is not fair enough to understand. i am afraid but you need to http://www.symfony-project.org. which means waste of time with this article :D

BEST OF LUCK :)


4 Comments so far
Leave a comment

WOW.. Really a sexy article. I like your way of thinking.. Keep up good work.

Comment by Rubel

@Rubel: thank you very much. i shall try to keep up. but BTW what’s my way of thinking ;) i am not sure :D

Comment by admin

http://boka.com

Comment by boka

This is quite a up-to-date info. I’ll share it on Facebook.

Comment by Vince Delmonte




Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>