A Quick & Dirty Guide to understanding Symfony Minimally !
Why this guide when the documentation at symfony site is complete? The answer to this question is that i tried reading the documentation at symfony site, but as always when i am reading any tech books( to me tech books are not thrillers like usual suspects nor unputdownable novels like godfather ), i got bored after a 20 couple of pages ( or 20 page clicks ).Then I decided to write my own write ups based on what i read/understand every day and update it on my blog.These writeups does'nt follow any order nor 100% complete for a particular topic rather it is composed of very loosely coupled information on how to use this particular framework.1 The symfony framework is a set of files written in PHP using the MVC standard based on Mojavi, ORM using Propel, DB abstraction using Creole.
2 Install using PEAR.
3 Initiate your project structure.
First you must have a project folder, say "projects", if not create the directory.
cd to that "projects" directory and type
$ symfony init-project projects
A file structure similar to this will be created under projects
apps/
batch/
cache/
config/
data/
doc/
lib/
log/
test/
web/
4 Next you need to create your applications under apps directory,
for that type $ symfony init-app musicSite
A file structure similar to this will be created under apps/musicSite
apps/
musicSite/
config/
i18n/
lib/
modules/
templates/
Root Structure
The batch directory - usually crons are put here.
The cache directory - each application will have a subdirectory here where the preprocessed HTML and configuration files are kept.
The config directory - general configuration files are kept here.
The data directory - used to store the db schema or table creator sql code here in the sql subdirectory.
The doc directory - stores the project docs in the api subdirectory.
The lib directory - stores the foreign classes or libraries where all apps can share common code.
The log directory - stores the log files generated directly by symfony or by web server or database server.
The test directory - contains unit tests written in PHP .
The web directory - contains css, images, js, user uploaded files under particular subdirectories.
web/
css/
images/
js/
uploads/
Finally the app directory - contains one directory for each application of the project, here we have one app right now called musicSite.
App Structure
The config directory - a set of YAML configuration files and other settings used through out the application.
The i18n directory - contains files used for the internationalization of the application.
The lib directory - contains classes and libraries that are specific to the application.
The modules directory - contains all the modules that contain the features of the application.
The templates directory - contains all the global templates of the application.
Module Structure
Each application contains one or more modules named as a subdirectory under modules, say shopping in my modules folder.
This is done by typing
$ symfony init-module musicSite shopping
ie., under
projects/
apps/
musicSite/
modules/
shopping/
actions/
actions.class.php
config/
lib/
templates/
indexSuccess.php
validate/
The actions directory - Under the actions directory a class called actions.class.php contains all the actions of the module.
The config directory - contains custom configuration files with local parameters for the module.
The lib directory - contains classes and libraries that are specific to the module.
The templates directory - contains the templates corresponding to the actions of the module.
The validate directory - contains configuration files used for form validation.
Note: for the above 3 symfony commands you must be in the symfony project directory, which is projects in this case.
5 Web server setup for adding virtual host and URL rewriting.
Symfony MVC architecture
Model represents the persistent data, namely the database .
View renders the model into a web page for the end user presentation.
Controller responds to user actions, and invokes changes on the model or view as appropriate.
In symfony, the Controller is separated in different parts, namely a front controller and a set of actions.
Actions are located inside
musicSite/frontEnd/modules/buyMusic/actions/ directory,
where the action class is named as action.class.php.
In order to call an action you need to pass the parameter 'module/action' to the front controller(FC):
http://myapp.example.com/index.php/module_name/action_name/
param-name/param-value
The FC is usually index.php located in myApp/web directory which is the unique entry point to the whole application.What it does is it will call the controller and dispatch the user request ( after any filter requests) to the particular module/action described above.The action process the requests ie., by accessing the Model, ( which can be a database select or update or delete ) and executes the appropriate view after the end of the execution of the action.
The different kind of action returns are
return sfView::TYPE; where TYPE can be
SUCCESS - which is the default view to call and most common
ERROR - which is the error view to call
NONE - which is the no view to call
You can also chain multiple actions by using
$this->forward('otherModule', 'index');
and can also redirect to another module by using
$this->redirect('/otherModule/index');
Always use redirect when the form action is "POST" for avoiding possible resubmitting the request accidently.
More about Super Models..Ahem
apps/
musicSite/
config/
i18n/
lib/
modules/
templates/
Root Structure
The batch directory - usually crons are put here.
The cache directory - each application will have a subdirectory here where the preprocessed HTML and configuration files are kept.
The config directory - general configuration files are kept here.
The data directory - used to store the db schema or table creator sql code here in the sql subdirectory.
The doc directory - stores the project docs in the api subdirectory.
The lib directory - stores the foreign classes or libraries where all apps can share common code.
The log directory - stores the log files generated directly by symfony or by web server or database server.
The test directory - contains unit tests written in PHP .
The web directory - contains css, images, js, user uploaded files under particular subdirectories.
web/
css/
images/
js/
uploads/
Finally the app directory - contains one directory for each application of the project, here we have one app right now called musicSite.
App Structure
The config directory - a set of YAML configuration files and other settings used through out the application.
The i18n directory - contains files used for the internationalization of the application.
The lib directory - contains classes and libraries that are specific to the application.
The modules directory - contains all the modules that contain the features of the application.
The templates directory - contains all the global templates of the application.
Module Structure
Each application contains one or more modules named as a subdirectory under modules, say shopping in my modules folder.
This is done by typing
$ symfony init-module musicSite shopping
ie., under
projects/
apps/
musicSite/
modules/
shopping/
actions/
actions.class.php
config/
lib/
templates/
indexSuccess.php
validate/
The actions directory - Under the actions directory a class called actions.class.php contains all the actions of the module.
The config directory - contains custom configuration files with local parameters for the module.
The lib directory - contains classes and libraries that are specific to the module.
The templates directory - contains the templates corresponding to the actions of the module.
The validate directory - contains configuration files used for form validation.
Note: for the above 3 symfony commands you must be in the symfony project directory, which is projects in this case.
5 Web server setup for adding virtual host and URL rewriting.
Symfony MVC architecture
Model represents the persistent data, namely the database .
View renders the model into a web page for the end user presentation.
Controller responds to user actions, and invokes changes on the model or view as appropriate.
In symfony, the Controller is separated in different parts, namely a front controller and a set of actions.
Actions are located inside
musicSite/frontEnd/modules/buyMusic/actions/ directory,
where the action class is named as action.class.php.
In order to call an action you need to pass the parameter 'module/action' to the front controller(FC):
http://myapp.example.com/index.php/module_name/action_name/
param-name/param-value
The FC is usually index.php located in myApp/web directory which is the unique entry point to the whole application.What it does is it will call the controller and dispatch the user request ( after any filter requests) to the particular module/action described above.The action process the requests ie., by accessing the Model, ( which can be a database select or update or delete ) and executes the appropriate view after the end of the execution of the action.
The different kind of action returns are
return sfView::TYPE; where TYPE can be
SUCCESS - which is the default view to call and most common
ERROR - which is the error view to call
NONE - which is the no view to call
You can also chain multiple actions by using
$this->forward('otherModule', 'index');
and can also redirect to another module by using
$this->redirect('/otherModule/index');
Always use redirect when the form action is "POST" for avoiding possible resubmitting the request accidently.
More about Super Models..Ahem
Symphony uses OO abstraction a.k.a., Object Relational Mapping so that you can access the database as objects and also without writting any SQL queries as such.To start ORM in your application, u have to first initialise the data model by editing the file schema.xml located in the config directory directly under the main application.
A sample partial schema.xml is given at the top of this post for your preservance of sanity.The above XML probably looks familiar to mysql users and is mostly intuitive.
A sample partial schema.xml is given at the top of this post for your preservance of sanity.The above XML probably looks familiar to mysql users and is mostly intuitive.