You are on page 1of 32

Developing with Symfony1.2, NetBeans6.

8 and PostgreSQL in Ubuntu


Linux

(a) Creating a database application using Symfony and NetBeans in Ubuntu

1. If you have gone through my first tutorial on Ubuntu i.e. ‘Installing PHP development
environment in Ubuntu’ then go to step 2 otherwise please check out first tutorial then
move forward.

2. Please download the symfony configuration checker script from http://sf-to.org/1.2/check.php


and paste it in Document Root of your Apache as following, please see
check_configuration.php in my public_html shown below:

3. Open above said file in your web browser with http://localhost/check_configuration.php. If


you have done installation as per tutorial ‘Installing PHP development environment in
Ubuntu’ you will got following output.
4. As per output please set short_open_tag and magic_quoted_gpc to off by editing php.ini.
Open file by typing following command in Terminal window:
$ sudo gedit /etc/php5/apache2/php.ini

Make the changes as following, then save the file and exit.
5. Please restart your Apache with following command:
$ sudo /etc/init.d/apahe2 restart

6. Again visit check_configuration url in your web browser using


http://localhost/check_configuration.php. The output must look like below:
7. For installation of Symfony on Ubuntu linux. Please download symfony-1.2.10.tgz from
http://www.symfony-project.org/installation /1_2. Unzip the folder by right click and Extract
Here option. Copy unzipped folder in Document Root of your system i.e. public_html as
shown below:
8. Please start NetBeans by clicking Applications  Programming  NetBeans IDE 6.8. Click
on options Tools  Options shown below:
9. Click on PHP  General and see that you PHP 5 Interpreter will look like as follows:

10. Then click on Symfony tab, and set path to bin directory of symfony as shown below:
11. Now its time to set keyboard shortcuts to Symfony Command Box and Clear Cache
command which we run most frequently. Click on Keymap and type run in Search box, it
will show Symfony: Run Command in Actions below. Please select the text and Press …
button in front of text and select Edit as shown below:

12. Press ‘Alt+L’ from keyboard and then press OK button on the following screen, to set the
shortcut for Symfony: Run Command.
13. symfony cc is the command we use most frequently. That’s why we are going to assign
keyboard shortcut to that also. Please click on Keymap and this time type clear cache in
Search box it will Symfony: Clear Cache in Actions. Please select and assign ‘Alt+C’ to this
as shown below:

14. Now your system is ready for development with Symfony and NetBeans. Start NetBeans and
Click on File  New Project. Creation of this project starts with following screen. Please
click on PHP  PHP Application and Next button.
15. Next screen will ask for Project Name give it myfirstproject. This tutorial is same as My
First Project given on Symfony’s website i.e. http://www.symfony-
project.org/get/pdf/my-first-project-1.2-en.pdf. Please note down Sources Folder it must
be Document Root of your Apache Server given as following:

16. Next screen will tell you about Run Configuration as following:
17. On next screen, please select Symfony PHP Web framework and give –csrf-secret as I have
given ‘navinder123’. Click on Finish button.

18. This will create a lot of directories as follows:


19. Please click on myfirstproject and press ‘Alt+L’. This will give you symfony command box.
Please type configure in Filter box. Click on configure:database in Matching Tasks and
give “pgsql:dbname=myfirstproject;host=localhost” postgres postgres in Parameters.
In above string:
a. pgsql – is driver to PostgreSQL
b. myfirstproject – is database you have in your postgresql
c. host – host on which you are creating
d. postgres – is username
e. postgres – is password
You should create your myfirstproject database by opening pgAdmin III. The screen for
configure:database will look like as following:

20. Above command will change your /myfirstproject/config/databases.yml. Look at your dsn,
username and password will changed according to configure:database command.
databases.yml will look like as following:
21. Now open /myfirstproject/config/schema.yml and create schema of your project as
following. Please take care of indentation and don’t use tab key while creating schema. First
you have to write connection name i.e. propel, then table names i.e. blog_post and
blog_comment further they hold fieldnames like blog_post has id, title, excerpt, body,
created_at.
22. Now schema is ready. Please create model from it. Press ‘Alt+L’ to invoke symfony
command box and run command:
propel:build-model

23. Above command will create a folder model in /myfirstproject/lib folder in project directory.
In model folder one each table has two classes one is object class that will hold get and set
methods and other is peer class this class includes database operations (i.e. insert, select,
delete). The files in /myfirstprojet/lib/model are blank, these files are for you, you can write
our customized code in these files. Take a look at created directories as below:
24. Now model is ready, you can create SQL from it by using propel:build-sql. Please press
‘Alt+L’ and give propel:build-sql command as following:
25. Above command will create a file lib.model.schema.sql in /myfirstproject/data/sql which
holds sql commands to create tables and set primary and foreign key given following:

26. SQL commands created in previous step will be executed by propel:insert-sql. Press ‘Alt+L’
and give command. NetBeans will ask for confirmation message. Please press ‘Y’ there.
27. Above command will create two tables i.e. blog_comment and blog_post in your
PostgreSQL. Please open pgAdmin III and see the tables in myfirstproject. It must be as
following:

28. Now your database and model is ready. Create forms using propel:build-forms.
Please press ‘Alt+L’ and give propel:build-forms as following:
29. Above command will create form folder in /myfirstproject/lib folder of project. As
we have discussed in Model creation each table create two forms one in base folder and other
outside base folder. You can customize form outside base folder according to your
requirement. Avoid customization of Base forms inside base folder because it will got
changed every time you execute propel:build-forms

30. Until now forms are ready. Now for database operation like Create, Read, Update,
Delete you have to create modules. Symfony has commands for that also, it will create your
CRUD operation automatically for you. Use following commands
propel:generate-module frontend post BlogPost
propel:generate-module frontend comment BlogComment

Please press ‘Alt+C’ to clear cache.


31. Above two commands will create two modules (i.e. two directories) in
/myfirstproject/apps/frontend/modules as following:
32. Each directory here contains two folders one is actions and another is templates.
Actions folder contains Controller and templates contain View of the module. Two folders in
post are shown as following:

33. Now the project is ready, we have to setup our web server. Please create an alias of
myfirstproject by editing /etc/apache2/sites-available/mysite. Open Terminal window give
following command:
$ sudo gedit /etc/apache2/sites-available/mysite

Before last line please add following, Save the file and Exit.
34. Please restart Apache using following command:
$ sudo /etc/init.d/apache2 restart

35. Now web server is also ready. Open your browser and give url as:
http://localhost/myfirstproject/frontend_dev.php
Output must be as following:

36. Now give this url: http://localhost/myfirstproject/frontend_dev.php/post. You will see


it is displaying contents of your blog_post table.
37. If we look at url we are using frontend_dev.php in it. What is it? It is front
controller to the development environment of Symfony. Symfony supports different
environments for different purposes:

• The development environment: This is the environment used by web developers


when they work on the application to add new features, fix bugs etc.
(frontend_dev.php)
• The test environment: This environment is used to automatically test the
application.
• The production environment: This is the environment end users interact with.
(index.php)

38. In step 36, you are giving http://localhost/myfirstproject/frontend_dev.php/post.


By default when we give a url it is as modulename/actionname but in above url you are
only giving module name i.e. post. When you do this Symfony will automatically take it as
post/index that means it is invoking index action of post module. It will go to controller (i.e.
/myfirstproject/apps/frontend/modules/post/actions/actions.class.php) of the post module
and go for executeIndex() function.

39. As mentioned in above step in executeIndex() function, it will put the contents of
blog_post table in blog_post_list array and give the control to indexSuccess.php template to
display contents of blog_post_list. The indexSuccess.php will look like this:
40. Anyway open browser and give url as
http://localhost/myfirstproject/frontend_dev.php/post
Click on New link, Fill the form as following and click on Save button to save the contents of
form.
41. After that click on Cancel to see the saved contents. The screen will look like as
following:

42. In next step. Please open second url as:


http://localhost/myfirstproject/frontend_dev.php/comment
Now click on New link. You will get an error like as following:
43. Above step is giving error because comment table will hold foreign key to blog_post. We
have not given the field for blog_post to return in blog_comment. For this please add a magic
method to BlogPost as following:
44. Refresh the page in step 42, You will get the comment form as following:

45. Save the content and Click Cancel to see the saved contents.
46. Now its time to format the contents of the project. Please open
/myfirstproject/web/css/main.css and add some css classes as following into it:
47. Now again open browser and give url as:
http://localhost/myfirstproject/frontend_dev.php/comment
You can see the effect of css classes on next screen:

48. You can change the layout of the project with single file. Please open
/myfirstproject/apps/frontend/templates/layout.php and change the body part of
layout.php as following:
49. Open the url given in step 47 again in browser and see the effect. The screen will look like as
following:

50. Now its time to create a starting module. Please create a new module by pressing ‘Alt+L’ and
give command:
generate:module frontend main
51. Please delete single line in executeIndex() of actions.class.php in
/myfirstproject/apps/frontend/modules/main/actions. The screen will look like as
following:

52. Please change indexSuccess.php of


/myfirstproject/apps/frontend/modules/main/templates to give a welcome message as
following:
53. Now set your starting module by editing /myfirstproject/apps/frontend/config/routing.yml
Change homepage as module: main. The file will look like as following:

:
54. Please clear cache by pressing ‘Alt+C’. Your application is ready, see it with :
http://localhost/myfirstproject/frontend_dev.php/main
You can navigate to List of Posts and List of Comments
55. Now project is ready. Its time to package the project for deployment. To package the project
use following command:
project:freeze /home/<user>/public_html/symfony-1.2.10/data/bin
The screen for above command is below:
56. Above command will add three folders i.e. /myfirstproject/data/symfony,
/myfirstproject/lib/symfony and /myfirstproject/web/sf into project as shown below. With
freeze command you are adding symfony library files to your project. Now your project
folder is ready to deploy.

57. That’s all the application is finished. With this tutorial I want to tell you about how to
develop a basic application using symfony and NetBeans. For detailed study you have to go
through Symfony book (http://www.symfony-project.org/get/pdf/book-1.2-en.pdf), Getting
Started (http://www.symfony-project.org/get/pdf/getting-started-1.2-en.pdf), My First Project
(http://www.symfony-project.org/get/pdf/my-first-project-1.2-en.pdf) and Forms in Action
(http://www.symfony-project.org/get/pdf/forms-1.2-en.pdf) given on Symfony website.

You might also like