ROUTING IN SYMFONY
Sayed Ahmed
B.Sc. Eng. in Computer Science & Engineering
M. Sc. in Computer Science
Exploring Computing for 14+ years
sayed@justetc.net
http://sayed.justetc.net
URLS
 Symfony has an entire framework dedicated to URL
management:
 the routing framework
 Exposing internal code structure or sensitive
information through URLs is not secure
 Hence, Symfony uses internal URIs and External URLs
 routing manages internal URIs and external URLs
 When a request comes in
 the routing parses the URL
 and converts it to an internal URI
10/8/2011
2
sayed@justetc.net
URLS
 Internal URI
 'job/show?id='.$job->getId()
 The generic pattern for internal URIs is:
 MODULE/ACTION?key=value&key_1=value_1&...
 The url_for() helper converts this internal URI to a
proper URL:
 External URL: /job/show/id/1
 Converting Internal URIs to External URLs
 url_for('@default?module=job&action=show&id='.$job-
>getId())
 url_for('job/show?id='.$job->getId())
10/8/2011
3
sayed@justetc.net
ROUTING CONFIGURATION
 The mapping between internal URIs and external
URLs is done in the routing.yml configuration file:
10/8/2011
4
sayed@justetc.net
ROUTE CUSTOMIZATIONS
For production environment, clear the cache by calling the cache:clear task.
10/8/2011
5
sayed@justetc.net
EXAMPLE: ROUTE CUSTOMIZATION
 /job/sensio-labs/paris-france/1/web-developer
 Pattern:
 /job/:company/:location/:id/:position
 routing.yml
Changes required in the web-page file
10/8/2011
6
sayed@justetc.net
EXAMPLE: ROUTE CUSTOMIZATION
 An internal URI can also be expressed as an array:
10/8/2011
7
sayed@justetc.net
ROUTING AND INPUT VALIDATION
 The routing system has a built-in validation feature
 Each pattern variable can be validated by
 a regular expression
 defined using the requirements entry of a route
definition
10/8/2011
8
sayed@justetc.net
ROUTE CLASS
 Each route defined in routing.yml is internally converted
to an object of class sfRoute
 Can be customized as follows:
 sfRequestRoute
 Can provide by blocking
 DELETE, and PUT http requests
10/8/2011
9
sayed@justetc.net
SHORTENING OF URL_FOR()
 Object Route Class
 Use sfDoctrineRoute
 class: sfDoctrineRoute
url_for('job/show?id='.$job->getId().'&company='.$job->getCompany().
'&location='.$job->getLocation().'&position='.$job->getPosition())
Can be shortened to
url_for(array('sf_route' => 'job_show_user', 'sf_subject' => $job))
Or
url_for('job_show_user', $job)
10/8/2011
10
sayed@justetc.net
MAKE THE URL LOOK PRETTY
 Convert
 http://www.jobeet.com.localhost/frontend_dev.php/job/S
ensio+Labs/Paris%2C+France/1/Web+Developer
 To
 http://www.jobeet.com.localhost/frontend_dev.php/job/se
nsio-labs/paris-france/1/web-developer
10/8/2011
11
sayed@justetc.net
10/8/2011
12
sayed@justetc.net
CHECK THE ROUTING
10/8/2011
13
sayed@justetc.net
ROUTING IN ACTIONS AND TEMPLATES
 Some symfony helpers take an internal URI as an
argument,
 link_to() for example
 <?php echo link_to($job->getPosition(), 'job_show_user',
$job) ?>
 Output
 <a href="/job/sensio-labs/paris-france/1/web-developer">Web
Developer</a>
 Generate absolute URL
 url_for('job_show_user', $job, true);
 link_to($job->getPosition(), 'job_show_user', $job, true);
 Generate url from action
 $this->redirect($this->generateUrl('job_show_user', $job));
10/8/2011
14
sayed@justetc.net
THE "REDIRECT" METHODS FAMILY
 redirect()
 redirectIf()
 redirectUnless()
10/8/2011
15
sayed@justetc.net
COLLECTION ROUTE CLASS
The job route above is really just a shortcut that automatically generates
seven sfDoctrineRoute routes: job, job_new, job_create, job_edit, job_update,
job_delete, job_show
10/8/2011
16
sayed@justetc.net
ROUTE DEBUGGING
 The app:routes task outputs all the routes for a
given application:
 $ php symfony app:routes frontend
 $ php symfony app:routes frontend job_edit
 Produces more debugging information
10/8/2011
17
sayed@justetc.net
DEFAULT ROUTES
 It is a good practice to define routes for all your
URLs
10/8/2011
18
sayed@justetc.net
REFERENCES
 http://www.symfony-project.org/jobeet/1_4/Doctrine/en/
10/8/2011
19
sayed@justetc.net

Routing in symfony

  • 1.
    ROUTING IN SYMFONY SayedAhmed B.Sc. Eng. in Computer Science & Engineering M. Sc. in Computer Science Exploring Computing for 14+ years sayed@justetc.net http://sayed.justetc.net
  • 2.
    URLS  Symfony hasan entire framework dedicated to URL management:  the routing framework  Exposing internal code structure or sensitive information through URLs is not secure  Hence, Symfony uses internal URIs and External URLs  routing manages internal URIs and external URLs  When a request comes in  the routing parses the URL  and converts it to an internal URI 10/8/2011 2 sayed@justetc.net
  • 3.
    URLS  Internal URI 'job/show?id='.$job->getId()  The generic pattern for internal URIs is:  MODULE/ACTION?key=value&key_1=value_1&...  The url_for() helper converts this internal URI to a proper URL:  External URL: /job/show/id/1  Converting Internal URIs to External URLs  url_for('@default?module=job&action=show&id='.$job- >getId())  url_for('job/show?id='.$job->getId()) 10/8/2011 3 sayed@justetc.net
  • 4.
    ROUTING CONFIGURATION  Themapping between internal URIs and external URLs is done in the routing.yml configuration file: 10/8/2011 4 sayed@justetc.net
  • 5.
    ROUTE CUSTOMIZATIONS For productionenvironment, clear the cache by calling the cache:clear task. 10/8/2011 5 sayed@justetc.net
  • 6.
    EXAMPLE: ROUTE CUSTOMIZATION /job/sensio-labs/paris-france/1/web-developer  Pattern:  /job/:company/:location/:id/:position  routing.yml Changes required in the web-page file 10/8/2011 6 sayed@justetc.net
  • 7.
    EXAMPLE: ROUTE CUSTOMIZATION An internal URI can also be expressed as an array: 10/8/2011 7 sayed@justetc.net
  • 8.
    ROUTING AND INPUTVALIDATION  The routing system has a built-in validation feature  Each pattern variable can be validated by  a regular expression  defined using the requirements entry of a route definition 10/8/2011 8 sayed@justetc.net
  • 9.
    ROUTE CLASS  Eachroute defined in routing.yml is internally converted to an object of class sfRoute  Can be customized as follows:  sfRequestRoute  Can provide by blocking  DELETE, and PUT http requests 10/8/2011 9 sayed@justetc.net
  • 10.
    SHORTENING OF URL_FOR() Object Route Class  Use sfDoctrineRoute  class: sfDoctrineRoute url_for('job/show?id='.$job->getId().'&company='.$job->getCompany(). '&location='.$job->getLocation().'&position='.$job->getPosition()) Can be shortened to url_for(array('sf_route' => 'job_show_user', 'sf_subject' => $job)) Or url_for('job_show_user', $job) 10/8/2011 10 sayed@justetc.net
  • 11.
    MAKE THE URLLOOK PRETTY  Convert  http://www.jobeet.com.localhost/frontend_dev.php/job/S ensio+Labs/Paris%2C+France/1/Web+Developer  To  http://www.jobeet.com.localhost/frontend_dev.php/job/se nsio-labs/paris-france/1/web-developer 10/8/2011 11 sayed@justetc.net
  • 12.
  • 13.
  • 14.
    ROUTING IN ACTIONSAND TEMPLATES  Some symfony helpers take an internal URI as an argument,  link_to() for example  <?php echo link_to($job->getPosition(), 'job_show_user', $job) ?>  Output  <a href="/job/sensio-labs/paris-france/1/web-developer">Web Developer</a>  Generate absolute URL  url_for('job_show_user', $job, true);  link_to($job->getPosition(), 'job_show_user', $job, true);  Generate url from action  $this->redirect($this->generateUrl('job_show_user', $job)); 10/8/2011 14 sayed@justetc.net
  • 15.
    THE "REDIRECT" METHODSFAMILY  redirect()  redirectIf()  redirectUnless() 10/8/2011 15 sayed@justetc.net
  • 16.
    COLLECTION ROUTE CLASS Thejob route above is really just a shortcut that automatically generates seven sfDoctrineRoute routes: job, job_new, job_create, job_edit, job_update, job_delete, job_show 10/8/2011 16 sayed@justetc.net
  • 17.
    ROUTE DEBUGGING  Theapp:routes task outputs all the routes for a given application:  $ php symfony app:routes frontend  $ php symfony app:routes frontend job_edit  Produces more debugging information 10/8/2011 17 sayed@justetc.net
  • 18.
    DEFAULT ROUTES  Itis a good practice to define routes for all your URLs 10/8/2011 18 sayed@justetc.net
  • 19.