CakePHP2.xCookBook Chinese version Chapter 3 Introduction to the structure of CakePHP

Source: Internet
Author: User
The names of these methods are enough to let you know what they do. You can find more detailed information in the model chapter. Structure of CakePHP

CakePHP includes controllers, models, and view classes, as well as additional classes and objects that make MVC programming faster and easier. Components, behaviors, and assistants are such classes that provide extensions and utilization so that you can quickly add features in MVC-based applications. Now we will stay at a high level, looking for details on how to use these jobs.

Application expansion

Each controller, helper, and model has a parent class that you can use to define changes in the application. AppController (in/app/Controller/AppController. php), AppHelper (in/app/View/Helper/AppHelper. php) and AppModel (in/app/Model/AppModel. php) is a good place to place the methods you want to share in all controllers, assistants, and models.

Although they are not classes or files, routes still play an important role in CakePHP requests. The routing definition tells CakePHP how to map URLs to controller actions. The default behavior assumes that URL/controller/action/var1/var2 is mapped to Controller: action ($ var1, $ var2 ), you can use custom routing URLs and how they are interpreted in your application.

Some application features deserve to be packaged as a whole. Ins are models, controllers, and view packages for specific purposes that span multiple applications. The user management system or simple blog is more suitable for CakePHP plug-ins.

Controller extension ("component ")

A component is a class that provides help in the controller logic. If you have some logic that you want to share among multiple controllers (or applications), components are usually appropriate. As an example, the kernel's EmailComponent class creates a component for creating and sending emails. Compared to writing a method in a single controller to implement this logic, you can package this logic and share it.

The controller is also suitable for Callback. These callbacks are also useful to you. in some cases, you need to insert some logic between the core operations of CakePHP. Valid callbacks include:

  • BeforeFilter (), executed before all controller action logic
  • BeforeRender (), executed after the controller logic and before the view rendering
  • AfterFilter () is executed after all controller logic (including view rendering. AfterRender () and afterFilter () are different until you manually call render () in the controller action, and some logic is included after the call.
Model Extension ("behavior ")

Simply put, a common function is added between models for behavior. For example, if you store User data in a tree structure, you can specify the behavior of your User model as a tree and provide the function of deleting, adding, and moving nodes in your tree structure.

The model always supports calling data sources from other classes. A data source is an abstract concept that allows a model to manipulate different types of data in a consistent manner. The main data source of the CakePHP application is often a database. you may write an additional data source that allows your model to provide RSS feeds, CSV files, LDAP branches, or iCal events. Data sources allow you to integrate data from different sources: compared with SQL connection restrictions, data sources allow you to integrate your LDAP model into many iCal events.

Like the controller, the model also contains callback:

  • BeforeFind ()
  • AfterFind ()
  • BeforeValidate ()
  • BeforeSave ()
  • AfterSave ()
  • BeforeDelete ()
  • AfterDelete ()

The names of these methods are enough to let you know what they do. You can find more detailed information in the model chapter.

View extension ("assistant ")

Assistant is a class that provides help in View logic. Similar to the controller, the assistant allows the visual logic to be accessed and shared among multiple views. JsHelper, one of the core assistants, makes it easier to use Ajax requests in views and provides jQuery (default), Prototype, and Mootools support.

More applications use View code snippets to provide reuse. CakePHP makes View code in layout and elements easier to reuse. By default, each view is rendered by the controller in a layout. Use an element when a small part of content is used in multiple views.

Related Article

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: 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.