Changeset 22363
- Timestamp:
- 09/24/09 16:52:01 (4 years ago)
- Files:
-
- doc/branches/1.3/jobeet/en/05.markdown (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
doc/branches/1.3/jobeet/en/05.markdown
r22355 r22363 14 14 15 15 If 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 willanswer all these questions.16 `/job/show/id/1`. If you have already developed PHP websites, you are probably 17 more accustomed to URLs like `/job.php?id=1`. How does symfony make it work? 18 How 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 20 answer all these questions. 21 21 22 22 But first, let's talk about URLs and what exactly they are. In a web context, … … 29 29 PHP language or that the job has a certain identifier in the database. 30 30 Exposing 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 resources32 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 t o their management: the **~routing|Routing~** framework. The routing manages internal37 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 32 resources he does not have access to? Sure, the developer must secure them the 33 proper way, but you'd better hide sensitive information. 34 35 URLs are so important in symfony that it has an entire framework dedicated to 36 their management: the **~routing|Routing~** framework. The routing manages 37 internal URIs and external URLs. When a request comes in, the routing parses 38 the URL and converts it to an internal URI. 39 39 40 40 You have already seen the internal URI of the job page in the … … 85 85 86 86 When 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 with87 route 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 89 means: Match a `/` followed by something. In our example, the `module` 90 variable will have `job` as a value. This value can then be retrieved with 91 91 `$request->getParameter('module')` in the action. This route also defines a 92 92 default value for the `action` variable. So, for all URLs matching this route, … … 127 127 128 128 For 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`: 129 congratulations 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 131 homepage. To make the change, modify the `module` variable of the `homepage` 132 route to `job`: 132 133 133 134 [php] … … 153 154 >When you update the routing configuration, the changes are immediately 154 155 >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. 156 158 157 159 For something a bit more involved, let's change the job page URL to something … … 173 175 /job/:company/:location/:id/:position 174 176 175 Edit the `routing.yml` file and add the `job_show_user` route at the beginning of the176 file:177 Edit the `routing.yml` file and add the `job_show_user` route at the beginning 178 of the file: 177 179 178 180 [yml] … … 204 206 ------------ 205 207 206 During the first day tutorial, we talked about validation and error 207 handlingfor good reasons. The routing system has a built-in ~validation|Validation~208 During the first day tutorial, we talked about validation and error handling 209 for good reasons. The routing system has a built-in ~validation|Validation~ 208 210 feature. Each pattern variable can be validated by a regular expression 209 defined using the `~requirements|Requirements~` entry of a ~route|Route~ definition: 211 defined using the `~requirements|Requirements~` entry of a ~route|Route~ 212 definition: 210 213 211 214 [yml] … … 226 229 can be changed by defining a `class` entry in the route definition. If you are 227 230 familiar 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 are230 supported by all browsers, while the other two are not.231 like `~GET|GET (HTTP Method)~`, `~POST|POST (HTTP Method)~`, `~HEAD|HEAD (HTTP 232 Method)~`, `~DELETE|DELETE (HTTP Method)~`, and `~PUT|PUT (HTTP Method)~`. The 233 first three are supported by all browsers, while the other two are not. 231 234 232 235 To restrict a route to only match for certain request methods, you can change … … 245 248 246 249 >**NOTE** 247 >Requiring a route to only match for some ~HTTP methods|HTTP Method~ is not totally248 > equivalent to using `sfWebRequest::isMethod()` in your actions. That's249 > because the routing will continue to look for a matching route if the250 >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 250 253 >method does not match the expected one. 251 254 … … 257 260 but as we have just learned in the previous section, the route class can be 258 261 changed. For the `job_show_user` route, it is better to use 259 [~`sfPropelRoute`~](http://www.symfony-project.org/api/1_3/sfPropelRoute) as the260 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 263 the class is optimized for routes that represent ##ORM## objects or 264 collections of ##ORM## objects: 262 265 263 266 [yml] … … 273 276 The `options` entry customizes the behavior of the route. Here, the `model` 274 277 option 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 use276 `list` if a route represents a collection of objects).278 the `type` option defines that this route is tied to one object (you can also 279 use `list` if a route represents a collection of objects). 277 280 278 281 The `job_show_user` route is now aware of its relation with `JobeetJob` and so … … 300 303 http://jobeet.localhost/frontend_dev.php/job/Sensio+Labs/Paris%2C+France/1/Web+Developer 301 304 302 We need to "~slug|Slug~ify" the column values by replacing all non ASCII characters303 by a `-`. Open the `JobeetJob` file and add the following methods to the 304 class:305 We need to "~slug|Slug~ify" the column values by replacing all non ASCII 306 characters by a `-`. Open the `JobeetJob` file and add the following methods 307 to the class: 305 308 306 309 [php] … … 370 373 http://jobeet.localhost/frontend_dev.php/job/sensio-labs/paris-france/1/web-developer 371 374 372 But that's only half the story. The route is able to generate a URL based on an373 object, but it is also able to find the object related to a given URL. The375 But that's only half the story. The route is able to generate a URL based on 376 an object, but it is also able to find the object related to a given URL. The 374 377 related object can be retrieved with the `getObject()` method of the route 375 378 object. When parsing an incoming request, the routing stores the matching … … 395 398  396 399 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: 400 That's because the ~404|404 Error~ error has been thrown for you automatically 401 by the `getRoute()` method. So, we can simplify the `executeShow` method even 402 more: 399 403 400 404 [php] … … 474 478 475 479 As 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 the477 `show` action. But as the `job` module defines the classic seven actions480 define a custom ~`sfPropelRoute`~ route for each as we have already done for 481 the `show` action. But as the `job` module defines the classic seven actions 478 482 possible for a model, we can also use the 479 483 [~`sfPropelRouteCollection`~](http://www.symfony-project.org/api/1_3/sfPropelRouteCollection) … … 565 569 >~HTTP method|HTTP Method~ requirements. 566 570 567 The `job_delete` and `job_update` routes requires ~HTTP methods|HTTP Method~ that are not568 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 t emplate to see an example:571 The `job_delete` and `job_update` routes requires ~HTTP methods|HTTP Method~ 572 that are not supported by browsers (`~DELETE|DELETE (HTTP Method)~` and 573 `~PUT|PUT (HTTP Method)~` respectively). This works because symfony simulates 574 them. Open the `_form.php` template to see an example: 571 575 572 576 [php] … … 607 611 -------------- 608 612 609 It is a good practice to define ~route|Route~s for all your URLs. As the `job` route610 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:613 It is a good practice to define ~route|Route~s for all your URLs. As the `job` 614 route defines all the routes needed to describe the Jobeet application, go 615 ahead and remove or comment the default routes from the `routing.yml` 616 configuration file: 613 617 614 618 [yml]