Changeset 11054
- Timestamp:
- 08/22/08 17:29:11 (3 months ago)
- Files:
-
- doc/branches/1.1/tutorial/my-first-project.txt (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
doc/branches/1.1/tutorial/my-first-project.txt
r10381 r11054 2 2 ======================== 3 3 4 So, you want to try it on? Let's build together a fully-functional web app in 5 one hour. You name it. A bookseller application? Ok, another idea. A blog! 4 >**Caution** 5 >This tutorial is presently being converted from the 1.0 version. It is entirely possible 6 >to get a basic version running on 1.1, but you may need to get a few hints from more 7 >experienced users in the [users' forum](http://www.symfony-project.org/forum/index.php/f/15/). 8 9 So, you want to give it a go? Let's build together a fully-functional web app in 10 one hour. You name it. A bookseller application? Okay, another idea. A blog! 6 11 That's a good one. Let's go. 7 12 8 We'll assume that you are working with Apache installed and launchedon your9 local host. You will also need PHP 5.1.3 or newer.13 We'll assume that you are working with Apache installed and running on your 14 local host. You will also need PHP 5.1.3 or newer. 10 15 11 16 Install symfony and initialize the project 12 17 ------------------------------------------ 13 18 14 To go fast, we will use the symfony sandbox. 15 16 It is an empty symfony project where all the required libraries are already included, 19 To go fast, we will use the symfony sandbox. This is an empty symfony project where all 20 the required libraries are already included, 17 21 and where the basic configuration is already done. The great advantage of the sandbox 18 22 over other types of installation is that you can start experimenting with symfony 19 23 immediately. 20 24 21 Get it here: [sf_sandbox_1_1.tgz](http://www.symfony-project.org/get/sf_sandbox_1_1.tgz), 22 and unpack it in your root web directory. It is recommended to keep the permissions as 23 they are in the tar file (for example by using -p with tar command). Refer to the 24 included `README` file for more information. The resulting file structure should look like: 25 Get it here: [sf_sandbox_1_1.tgz](http://www.symfony-project.org/get/sf_sandbox_1_1.tgz) 26 or here: [sf_sandbox_1_1.zip](http://www.symfony-project.org/get/sf_sandbox_1_1.zip), 27 and unpack it in your root web directory. On Linux systems, it is recommended to keep the 28 permissions as they are in the tar file (for example by using -p with tar command). 29 Refer to the included `README` file for more information. The resulting file structure 30 should look like this: 25 31 26 32 www/ … … 28 34 apps/ 29 35 frontend/ 30 batch/31 36 cache/ 32 37 config/ 33 38 data/ 34 sql/35 39 doc/ 36 40 lib/ 37 model/38 41 log/ 39 42 plugins/ … … 65 68 66 69 propel: 67 post:68 id: ~69 title: varchar(255)70 excerpt: longvarchar71 body: longvarchar72 created_at: ~73 comment:74 id: ~75 post_id:~76 author: varchar(255)77 email: varchar(255)78 body: longvarchar79 created_at: ~70 blog_post: 71 id: ~ 72 title: varchar(255) 73 excerpt: longvarchar 74 body: longvarchar 75 created_at: ~ 76 blog_comment: 77 id: ~ 78 blog_post_id: ~ 79 author: varchar(255) 80 email: varchar(255) 81 body: longvarchar 82 created_at: ~ 80 83 81 84 This configuration file uses the YAML syntax. It's a very simple language that … … 112 115 located in `sf_sandbox/data/`. 113 116 114 If you want to switch to MySQL for this project, just type the following command line:117 If you want to switch to MySQL for this project, use the `configure:database` task: 115 118 116 119 $ php symfony configure:database mysql://root:pa$$word@localhost/symfony_project … … 120 123 the [model chapter](http://www.symfony-project.org/book/1_1/08-Inside-the-Model-Layer)). 121 124 125 >**Caution** 126 >If you use SQLite as your database engine, you will have to change some rights on *nix systems: 127 > 128 > $ chmod 777 data data/sandbox.db 129 122 130 Now type in the command line: 123 131 … … 128 136 the same table structure. 129 137 138 >**Note** 139 >At the time of writing, the file path configured for the sqlite database file in the 140 >1.1 sandbox is not set up correctly. This however is easily fixed: just open up 141 >`/config/propel.ini` and ensure that the following lines have 7 sets of '..' in them: 142 > 143 >propel.database.createUrl = sqlite://./../../../../../../../data/sandbox.db 144 >propel.database.url = sqlite://./../../../../../../../data/sandbox.db 145 > 146 >If they do not, copy the lines as they appear here, and save the file. 147 130 148 To build the table structure based on the the SQL file, type: 131 149 … … 139 157 generate some forms based on our model schema: 140 158 141 $ php symfony propel:build-form 159 $ php symfony propel:build-forms 142 160 143 161 This task generates classes in the `sf_sandbox/lib/form/` directory. … … 153 171 interface automatically: 154 172 155 $ php symfony propel:generate-crud frontend post Post --with-show156 $ php symfony propel:generate-crud frontend comment Comment --with-show173 $ php symfony propel:generate-crud --non-verbose-templates --non-atomic-actions --with-show frontend post BlogPost 174 $ php symfony propel:generate-crud --non-verbose-templates --non-atomic-actions frontend comment BlogComment 157 175 $ php symfony cache:clear 158 176 159 On *nix systems, you will have to change some rights: 160 $ chmod 777 data 161 $ chmod 777 data/sandbox.db 177 >**Tip** 178 >When using the `propel:generate-crud` task, we have used the `--non-verbose-templates` 179 >and `--non-atomic-actions` options. If you want to learn the meaning of the available 180 >arguments and options for a given task, you can use the special `help` task: 181 > 182 > $ php symfony help propel:generate-crud 162 183 163 184 You now have two modules (`post` and `comment`) that will let you manipulate 164 objects of the ` Post` and `Comment` classes. A **module** usually represents a185 objects of the `BlogPost` and `BlogComment` classes. A **module** usually represents a 165 186 page or a group of pages with a similar purpose. Your new modules are located 166 187 in the `sf_sandbox/apps/frontend/modules/` directory, and they are accessible … … 170 191 http://localhost/sf_sandbox/web/frontend_dev.php/comment 171 192 172 Feel free to create a new post to make the blog look less empty. 193 If you try to create a comment, you will have an error because symfony doesn't yet 194 know how to convert a post object to a string. Edit the `BlogPost` class 195 (`lib/model/BlogPost.php`) and add the `__toString()` method: 196 197 [php] 198 class BlogPost extends BaseBlogPost 199 { 200 public function __toString() 201 { 202 return $this->getTitle(); 203 } 204 } 205 206 Now, feel free to create a new post to make the blog look less empty. 173 207 174 208  … … 179 213 (project, application, module). 180 214 181 >**Note**: In the URLs above, the name of the main script - called 215 >**Note**: In the URLs above, the name of the main script - called the 182 216 >*front controller* in symfony - was changed from `index.php` to `frontend_dev.php`. 183 217 >The two scripts access the same application (`frontend`), but in different environments. … … 222 256 </div> 223 257 224 Please be forgiving forthe poor design and the use of inner-tag css, but225 one hour is a short time.258 Please forgive the poor design and the use of inner-tag css, but 259 one hour is rather a short amount of time! 226 260 227 261  … … 237 271 metas: 238 272 title: The best blog ever 239 robots: index, follow240 273 description: symfony project 241 274 keywords: symfony, project 242 275 language: en 276 robots: index, follow 243 277 244 278 The home page itself needs to be changed. It uses the default template of the 245 279 `default` module, which is kept in the framework but not in your application 246 directory. To override it, you have tocreate a custom `main` module:280 directory. To override it, you can create a custom `main` module: 247 281 248 282 $ php symfony generate:module frontend main … … 253 287 254 288 [php] 255 public function executeIndex() 289 /** 290 * Executes index action 291 * 292 * @param sfRequest $request A request object 293 */ 294 public function executeIndex($request) 256 295 { 257 296 } … … 265 304 266 305 Now, you must tell symfony which action to execute when the homepage is requested. 267 To that extend, edit the `sf_sandbox/apps/frontend/config/routing.yml` and change306 To do so, edit the `sf_sandbox/apps/frontend/config/routing.yml` and change 268 307 the `homepage` rule as follows: 269 308 … … 279 318  280 319 281 Go ahead, start using your new web app : Create a new test post, and a test282 comment for your thispost.320 Go ahead, start using your new web app. Make sure you've created a test post, and 321 also create a test comment against your post. 283 322 284 323 Find more about [views and templates](http://www.symfony-project.org/book/1_1/07-Inside-the-View-Layer). … … 298 337 public function executeShow($request) 299 338 { 300 $this-> post =PostPeer::retrieveByPk($request->getParameter('id'));301 $this->forward404Unless($this-> post);339 $this->blog_post = BlogPostPeer::retrieveByPk($request->getParameter('id')); 340 $this->forward404Unless($this->blog_post); 302 341 303 342 $c = new Criteria(); 304 $c->add( CommentPeer::POST_ID, $request->getParameter('id'));305 $c->addAscendingOrderByColumn( CommentPeer::CREATED_AT);306 $this->comments = CommentPeer::doSelect($c);343 $c->add(BlogCommentPeer::BLOG_POST_ID, $request->getParameter('id')); 344 $c->addAscendingOrderByColumn(BlogCommentPeer::CREATED_AT); 345 $this->comments = BlogCommentPeer::doSelect($c); 307 346 } 308 347 309 348 The `Criteria` and `-Peer` objects are part of Propel's object-relational mapping. 310 Basically, these four lines will handle a SQL query to the ` Comment` table to get311 the comments related to the current ` Post` (the one designated by the URL parameter `id`).349 Basically, these four lines will handle a SQL query to the `blog_comment` table to get 350 the comments related to the current `blog_post` (the one designated by the URL parameter `id`). 312 351 The `$this->comments` line in the action will give access to a `$comments` variable 313 352 in the corresponding template. Now, modify the post display template … … 315 354 316 355 [php] 317 ...356 // ... 318 357 <?php use_helper('Text', 'Date') ?> 319 358 320 359 <hr /> 321 360 <?php if ($comments) : ?> 322 <p><?php echo count($comments) ?> comment <?php if (count($comments) > 1) : ?>s<?php endif; ?>to this post.</p>361 <p><?php echo count($comments) ?> comments to this post.</p> 323 362 <?php foreach ($comments as $comment): ?> 324 363 <p><em>posted by <?php echo $comment->getAuthor() ?> on <?php echo format_date($comment->getCreatedAt()) ?></em></p> … … 347 386 -------------------------------------- 348 387 388 >**Caution** 389 >The following section refers to code that would appear in the 1.0 version of the 390 >sandbox, however symfony 1.1 uses a new Forms system. It will take a while longer 391 >to convert this section. Please bear with us! 392 349 393 When adding a comment, you can choose the `id` of the related post. 350 394 That's not very user-friendly. Let's change this, and make sure that the user 351 395 comes back to the post he was looking at after adding a comment. 352 396 353 First, in the still fresh`modules/post/templates/showSuccess.php` template,397 First, in the `modules/post/templates/showSuccess.php` template, 354 398 add a line at the bottom: 355 399 356 400 [php] 357 <?php echo link_to('Add a comment', 'comment/ create?post_id='.$post->getId()) ?>358 359 The `link_to()` helper creates a hyperlink pointing to the ` create` action of401 <?php echo link_to('Add a comment', 'comment/edit?post_id='.$blog_post->getId()) ?> 402 403 The `link_to()` helper creates a hyperlink pointing to the `edit` action of 360 404 the `comment` module, so you can add a comment directly from the post details page. 361 405 Next, open the `modules/comment/templates/editSuccess.php` and replace the … … 678 722 add to the `sf_sandbox/lib/model/Post.php`: 679 723 680 [php] 681 function __toString() 682 { 683 return $this->getTitle(); 684 } 685 724 [php] 686 725 public function getNbComments() 687 726 {