Development

Changeset 22363

You must first sign up to be able to contribute.

Changeset 22363

Show
Ignore:
Timestamp:
09/24/09 16:52:01 (4 years ago)
Author:
fabien
Message:

[doc] [1.3] updated day 5

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/branches/1.3/jobeet/en/05.markdown

    r22355 r22363  
    1414 
    1515If you click on a job on the Jobeet homepage, the URL looks like this: 
    16 `/job/show/id/1`. If you have already developed PHP websites, you are 
    17 probably more accustomed to URLs like `/job.php?id=1`. How does symfony make 
    18 it work? How does symfony determines the action to call based on this URL? 
    19 Why is the `id` of the job retrieved with `$request->getParameter('id')`? 
    20 Today, we will answer all these questions. 
     16`/job/show/id/1`. If you have already developed PHP websites, you are probably 
     17more accustomed to URLs like `/job.php?id=1`. How does symfony make it work? 
     18How does symfony determines the action to call based on this URL? Why is the 
     19`id` of the job retrieved with `$request->getParameter('id')`? Today, we will 
     20answer all these questions. 
    2121 
    2222But first, let's talk about URLs and what exactly they are. In a web context, 
     
    2929PHP language or that the job has a certain identifier in the database. 
    3030Exposing the internal workings of your application is also quite bad as far as 
    31 ~security|Security~ is concerned: What if the user tries to guess the URL for resources 
    32 he does not have access to? Sure, the developer must secure them the proper 
    33 way, but you'd better hide sensitive information. 
    34  
    35 URLs are so important in symfony that it has an entire framework dedicated 
    36 to their management: the **~routing|Routing~** framework. The routing manages internal 
    37 URIs and external URLs. When a request comes in, the routing parses the URL 
    38 and converts it to an internal URI. 
     31~security|Security~ is concerned: What if the user tries to guess the URL for 
     32resources he does not have access to? Sure, the developer must secure them the 
     33proper way, but you'd better hide sensitive information. 
     34 
     35URLs are so important in symfony that it has an entire framework dedicated to 
     36their management: the **~routing|Routing~** framework. The routing manages 
     37internal URIs and external URLs. When a request comes in, the routing parses 
     38the URL and converts it to an internal URI. 
    3939 
    4040You have already seen the internal URI of the job page in the 
     
    8585 
    8686When you request the Jobeet homepage, which has the `/job` URL, the first 
    87 route that matches is the `default_index` one. In a pattern, a word ~prefixed|Prefix~ 
    88 with a colon (`:`) is a variable, so the `/:module` pattern means: Match a `/` 
    89 followed by something. In our example, the `module` variable will have `job
    90 as a value. This value can then be retrieved with 
     87route that matches is the `default_index` one. In a pattern, a word 
     88~prefixed|Prefix~ with a colon (`:`) is a variable, so the `/:module` pattern 
     89means: Match a `/` followed by something. In our example, the `module
     90variable will have `job` as a value. This value can then be retrieved with 
    9191`$request->getParameter('module')` in the action. This route also defines a 
    9292default value for the `action` variable. So, for all URLs matching this route, 
     
    127127 
    128128For now, when you request the `/` URL in a browser, you have the default 
    129 congratulations page of symfony. That's because this URL matches the `homepage` 
    130 ~route|Route~. But it makes sense to change it to be the Jobeet homepage. To make the 
    131 change, modify the `module` variable of the `homepage` route to `job`: 
     129congratulations page of symfony. That's because this URL matches the 
     130`homepage` ~route|Route~. But it makes sense to change it to be the Jobeet 
     131homepage. To make the change, modify the `module` variable of the `homepage` 
     132route to `job`: 
    132133 
    133134    [php] 
     
    153154>When you update the routing configuration, the changes are immediately 
    154155>taken into account in the development environment. But to make them 
    155 >also work in the production environment, you need to clear the cache. 
     156>also work in the production environment, you need to clear the cache by 
     157>calling the `cache:clear` task. 
    156158 
    157159For something a bit more involved, let's change the job page URL to something 
     
    173175    /job/:company/:location/:id/:position 
    174176 
    175 Edit the `routing.yml` file and add the `job_show_user` route at the beginning of the 
    176 file: 
     177Edit the `routing.yml` file and add the `job_show_user` route at the beginning 
     178of the file: 
    177179 
    178180    [yml] 
     
    204206------------ 
    205207 
    206 During the first day tutorial, we talked about validation and error 
    207 handling for good reasons. The routing system has a built-in ~validation|Validation~ 
     208During the first day tutorial, we talked about validation and error handling 
     209for good reasons. The routing system has a built-in ~validation|Validation~ 
    208210feature. Each pattern variable can be validated by a regular expression 
    209 defined using the `~requirements|Requirements~` entry of a ~route|Route~ definition: 
     211defined using the `~requirements|Requirements~` entry of a ~route|Route~ 
     212definition: 
    210213 
    211214    [yml] 
     
    226229can be changed by defining a `class` entry in the route definition. If you are 
    227230familiar with the ~HTTP~ protocol, you know that it defines several "methods", 
    228 like `~GET|GET (HTTP Method)~`, `~POST|POST (HTTP Method)~`, `~HEAD|HEAD (HTTP Method)~`,  
    229 `~DELETE|DELETE (HTTP Method)~`, and `~PUT|PUT (HTTP Method)~`. The first three ar
    230 supported by all browsers, while the other two are not. 
     231like `~GET|GET (HTTP Method)~`, `~POST|POST (HTTP Method)~`, `~HEAD|HEAD (HTTP 
     232Method)~`, `~DELETE|DELETE (HTTP Method)~`, and `~PUT|PUT (HTTP Method)~`. Th
     233first three are supported by all browsers, while the other two are not. 
    231234 
    232235To restrict a route to only match for certain request methods, you can change 
     
    245248 
    246249>**NOTE** 
    247 >Requiring a route to only match for some ~HTTP methods|HTTP Method~ is not totally 
    248 >equivalent to using `sfWebRequest::isMethod()` in your actions. That's 
    249 >because the routing will continue to look for a matching route if the 
     250>Requiring a route to only match for some ~HTTP methods|HTTP Method~ is not 
     251>totally equivalent to using `sfWebRequest::isMethod()` in your actions. 
     252>That's because the routing will continue to look for a matching route if the 
    250253>method does not match the expected one. 
    251254 
     
    257260but as we have just learned in the previous section, the route class can be 
    258261changed. For the `job_show_user` route, it is better to use 
    259 [~`sfPropelRoute`~](http://www.symfony-project.org/api/1_3/sfPropelRoute) as the 
    260 class is optimized for routes that represent ##ORM## objects or collections of 
    261 ##ORM## objects: 
     262[~`sfPropelRoute`~](http://www.symfony-project.org/api/1_3/sfPropelRoute) as 
     263the class is optimized for routes that represent ##ORM## objects or 
     264collections of ##ORM## objects: 
    262265 
    263266    [yml] 
     
    273276The `options` entry customizes the behavior of the route. Here, the `model` 
    274277option defines the ##ORM## model class (`JobeetJob`) related to the route, and 
    275 the `type` option defines that this route is tied to one object (you can also use 
    276 `list` if a route represents a collection of objects). 
     278the `type` option defines that this route is tied to one object (you can also 
     279use `list` if a route represents a collection of objects). 
    277280 
    278281The `job_show_user` route is now aware of its relation with `JobeetJob` and so 
     
    300303    http://jobeet.localhost/frontend_dev.php/job/Sensio+Labs/Paris%2C+France/1/Web+Developer 
    301304 
    302 We need to "~slug|Slug~ify" the column values by replacing all non ASCII characters 
    303 by a `-`. Open the `JobeetJob` file and add the following methods to the 
    304 class: 
     305We need to "~slug|Slug~ify" the column values by replacing all non ASCII 
     306characters by a `-`. Open the `JobeetJob` file and add the following methods 
     307to the class: 
    305308 
    306309    [php] 
     
    370373    http://jobeet.localhost/frontend_dev.php/job/sensio-labs/paris-france/1/web-developer 
    371374 
    372 But that's only half the story. The route is able to generate a URL based on an 
    373 object, but it is also able to find the object related to a given URL. The 
     375But that's only half the story. The route is able to generate a URL based on 
     376an object, but it is also able to find the object related to a given URL. The 
    374377related object can be retrieved with the `getObject()` method of the route 
    375378object. When parsing an incoming request, the routing stores the matching 
     
    395398![404 with sfPropelRoute](http://www.symfony-project.org/images/jobeet/1_3/05/404_propel_route.png) 
    396399 
    397 That's because the ~404|404 Error~ error has been thrown for you automatically by the 
    398 `getRoute()` method. So, we can simplify the `executeShow` method even more: 
     400That's because the ~404|404 Error~ error has been thrown for you automatically 
     401by the `getRoute()` method. So, we can simplify the `executeShow` method even 
     402more: 
    399403 
    400404    [php] 
     
    474478 
    475479As all `job` actions are related to the `JobeetJob` model class, we can easily 
    476 define a custom ~`sfPropelRoute`~ route for each as we have already done for the 
    477 `show` action. But as the `job` module defines the classic seven actions 
     480define a custom ~`sfPropelRoute`~ route for each as we have already done for 
     481the `show` action. But as the `job` module defines the classic seven actions 
    478482possible for a model, we can also use the 
    479483[~`sfPropelRouteCollection`~](http://www.symfony-project.org/api/1_3/sfPropelRouteCollection) 
     
    565569>~HTTP method|HTTP Method~ requirements. 
    566570 
    567 The `job_delete` and `job_update` routes requires ~HTTP methods|HTTP Method~ that are not 
    568 supported by browsers (`~DELETE|DELETE (HTTP Method)~` and `~PUT|PUT (HTTP Method)~`  
    569 respectively). This works because symfony simulates them. Open the `_form.php`  
    570 template to see an example: 
     571The `job_delete` and `job_update` routes requires ~HTTP methods|HTTP Method~ 
     572that are not supported by browsers (`~DELETE|DELETE (HTTP Method)~` and 
     573`~PUT|PUT (HTTP Method)~` respectively). This works because symfony simulates 
     574them. Open the `_form.php` template to see an example: 
    571575 
    572576    [php] 
     
    607611-------------- 
    608612 
    609 It is a good practice to define ~route|Route~s for all your URLs. As the `job` route 
    610 defines all the routes needed to describe the Jobeet application, go ahead and 
    611 remove or comment the default routes from the `routing.yml` configuration 
    612 file: 
     613It is a good practice to define ~route|Route~s for all your URLs. As the `job` 
     614route defines all the routes needed to describe the Jobeet application, go 
     615ahead and remove or comment the default routes from the `routing.yml` 
     616configuration file: 
    613617 
    614618    [yml]