CakePHP2.xCookBook Chapter 5 controller scaffold

Source: Internet
Author: User
If you want to rotate something different in your scaffolding view, you can create a template. Although we do not recommend using this technology to build applications, this custom feature is still useful in the prototype iteration phase. Scaffolding

Application scaffolding is a technique that allows a program to define and create an application with the ability to create, query, update, and delete objects. CakePHP scaffolding also allows programmers to define how objects interact with other objects and establish or disconnect them.

Scaffolding is used to create a model and its controller. Once you set the $ scaffold variable in the controller, you can run it.

The scaffolding of CakePHP is very cool. It allows you to complete a basic CRUD application within a few minutes. It is even cool to use it in product applications. Yes, we also think it is cool, but remember it.OnlyYes.

It is a loose structure that can be discarded at any time to quickly start a project. It is incomplete and not flexible, just a temporary solution for starting and running. If you find that you want to customize the logic or view, it is time to remove the scaffold and start writing your own code. CakePHP's Bake console (see the next section) is a good choice for the next step: it generates the same code as the current scaffold result (or more.

Scaffolding is a good way to start writing early code for a web application. The early database structure is changed at any time, which is completely normal at the initial stage of the design process. The negative impact is that web programmers hate to write a form that seems to never be used. To reduce the unnecessary work of programmers, CakePHP adds scaffolding. Scaffolding analyzes database tables and creates a standard list with buttons for adding, deleting, and editing; 2. standard editing form; 3. standard view for interaction with individual members of the database.

To add scaffolding to an application, add the $ scaffold variable to the controller:

1 class CategoriesController extends AppController { 2 public $scaffold; 3 }

Suppose you have created more basic Category Model class files (in/app/Model/Category. php), visit the http://example.com/categories to view your new scaffolding.

Annotation

Creating a method in a controller constructed with scaffolding may result in unexpected results. For example, if you create the index () method in the scaffold controller, your index method will be rendered preferentially before the scaffolding function is rendered.

Scaffolding understands the relationship between models. Therefore, if your Category model belongs to a User model (belonsto), you will see the associated User ID in the Category list. Although scaffolding "knows" the relationship between models, you still cannot see any association records in the scaffold view until you manually add Association code to the model. For example, if Group hasw.user and User blongsTo Group, you must manually add the following code in the User and Group models. Before you add the code, the view will display an empty Group drop-down list box in the New User form. After you add the code, the view will display a drop-down list consisting of IDs or names from the Group table in the New User form:

1 // In Group.php 2 public $hasMany = 'User'; 3 // In User.php 4 public $belongsTo = 'Group';

If you want to see more things (such as the user's surname) in an ID, you can set the $ displayField variable in the model. Let's set the $ displayField variable in our User class to display the name of the User associated with categories in the scaffold, instead of the ID. This feature makes scaffolding more readable in many instances:

1 class User extends AppModel { 2 public $displayField = 'first_name'; 3 }

Use scaffolding to create a simple management interface

If you are in app/Config/core. if you set the allow admin route in php, you can use the route with Configure: write ('routing. prefixes ', array ('admin'); to establish a management interface.

Once you allow the admin route, you only need to assign the admin prefix to the scaffold variable:

1 public $scaffold = 'admin';

You can access the admin scaffold action:

http://example.com/admin/controller/indexhttp://example.com/admin/controller/view http://example.com/admin/controller/edithttp://example.com/admin/controller/addhttp://example.com/admin/controller/delete

This method can quickly create a simple background interface. You cannot use both admin and non-admin methods in scaffolding. In normal scaffolding, you can overwrite or replace the following methods:

1 public function admin_view($id = null) { 2 // custom code here 3 }

Once you replace the scaffolding action, you also need to create a view file for this action.

Custom scaffolding view

If you want to rotate something different in your scaffolding view, you can create a template. Although we do not recommend using this technology to build applications, this custom feature is still useful in the prototype iteration phase.

Customize the scaffold view (such as PostsController) of the specified controller. the file location and name are similar:

/app/View/Posts/scaffold.index.ctp /app/View/Posts/scaffold.form.ctp /app/View/Posts/scaffold.view.ctp

To customize the scaffolding view in all controllers, the file location and name are similar:

/app/View/Scaffolds/index.ctp /app/View/Scaffolds/form.ctp /app/View/Scaffolds/view.ctp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.