According to the naming conventions, the database table posts should have a Post model class, which is defined in app/models/post. php. let's understand the code in the Blog example (no $ scaffold is used)
- According to the naming conventions, the database table posts should have a Post model class, which is defined in app/models/post. php.
- Create a control class in app/controllers/posts_controller.php. PostsController uses the Post model class.
- If the index action is defined in the control class PostsController, its output is defined in app/views/posts/index. thtml.
- Directory structure
App
- Models
- Controllers
- Views
- Posts
- Index. thtml
- View. thtml
- Edit. thtml
- Add. thtml
- ...
- Layouts
- Therefore, the request http ://... /posts/index: use app/controllers/(note that the two directories cannot be listed in the request path) to control the action index of PostsController, and use app/views/posts/index. thtml response return display
- The render Method is automatically called at the end of the action index to display the index. thtml view with the default layout default. thtml.
- In addition to defining the header and footer of the page, the default. thtml layout includes echo $ content_for_layout, where the index. thtml is actually displayed.
- In app/views/layouts/default. thtml, you can customize the layout to replace the default layout.
- Key code in the action index $ this-> set ('posts', $ this-> Post-> findAll ());
- Is to set the value of the variable named posts in the index. thtml view.
- $ This-> Post is a Post model class according to naming conventions.
- In this way, the array variable $ posts in the view index. thtml obtains all the records of the database table posts through the model class Post.
- Similarly, request http ://... /posts/view/3 will find the control class PostsController action view for processing, using app/views/posts/view. the thtml response is displayed, but the $ id value of the action parameter is 3.
- Code $ this-> set ('posts', $ this-> Post-> read (null, $ id) in the action view; set the variable $ posts in the view. thtml view
- View. code in thtml, such as echo $ posts ['post'] ['title'], shows the title value of the field with the id 3 recorded in the posts table (warm reminder that the middle Post is an uppercase P)
- Code $ this-> Post-> save ($ this-> data) is used to save the parameters of the control class through the model class Post. $ this-> data of params
- $ This-> data is the alias of $ this-> params ['data'], which refers to the data submitted by form POST.
.: $ Html ::.
Example
- Perhaps the hyperlink is simpler echo $ html-> link ('hyperlink text', 'hyperlink address ')
- If the current view is in the app/views/posts/directory, the 'hyperlink address' is the relative address of the current view directory.
- It is not difficult to insert an image echo $ html-> image ('image address', array ('alt' => "", 'border' => "0 "))
- The image is stored in the app/webroot/img/Directory. the 'image address' is the relative address of the image storage directory.
- Put the image link attribute in the array
- Using images as hyperlinks is a little complicated. for details about the parameters, see the Cake manual echo $ html-> link ($ html-> image ('image address'), 'hyperlink address ', null, null, false)