CakePHP 2.x CookBook Chinese version Fifth Chapter Controller Component _php Tutorial

Source: Internet
Author: User
A component component is a logical package that is shared across multiple controllers. If people find themselves wanting to copy and paste something between controllers, you should consider wrapping some functionality in one component. CakePHP also comes with a beautiful set of core components that are useful to you: security (Secure) Sessions (session) Access control Lists (access control list) Emails (email) cookiesauthentication (permission check) request handling (requests processing) pagination (paging) the details of these components are in their respective chapters. Now, we'll show you how to build your own components. Creating components keeps the controller code clean and allows you to reuse code across multiple projects. Configuration components Some core components need to be configured. Components that need to be configured are licensed, cookies, and e-mail components. For a generic component, it is usually configured in the $components array or the controller's Beforefilter method: 1 class Postscontroller extends AppController {2 public $components = Array (3 ' Auth ' = = Array (4 ' authorize ' = = Array (' controller '), 5 ' loginaction ' = = Array (' controller ' = + ' user S ', ' action ' = ' login ') 6), 7 ' Cookie ' = = Array (' name ' = ' cookiemonster ') 8); This is an example of configuring components using $components arrays. All core components are allowed to be configured in this way. In addition, you can configure the components in the Beforefilter () method of the controller. This is usually used in cases where you need to assign the result of a function to a component property. The above example can also be represented as: 1 public Function Beforefilter () {2 $this->auth->authorize = Array (' controller '); 3 $this->auth- >loginaction = Array (' controller ' = ' users ', ' action ' = ' login '); 4 5 $this->cooKie->name = ' Cookiemonster '; 6} However, there is also the possibility that a specific configuration option for a component should be set before the controller's Beforefilter () is run. Finally, some components allow configuration options to be set in the $components array: 1 Public $components = Array (2 ' debugkit.toolbar ' = = Array (' panels ' = = Array (' hist Ory ', ' Session ')) 3); Check the relevant documentation to determine which configuration options are available for each component. ClassName is a common set-up option that you can use to give your component an individual name. This option is useful when you want to replace $this->auth or other common components with a custom implementation. 1//app/controller/postscontroller.php 2 class Postscontroller extends AppController {3 Public $components = Array (4 ' A Uth ' = = Array (5 ' className ' = ' myauth ' 6) 7); 8} 9//App/controller/component/myauthcomponent.php11 app::uses (' authcomponent ', ' controller/component '); Myauthcomponent extends Authcomponent {//Add your code to override the core AuthComponent14} on the controller in the example above $this->auth Name is myauthcomponent. Annotations use aliases, including references within other components, when using any component that has an alias. Using components once you have included some components in the controller, it is very simple to use. Each component in the controller is used in the same way as a property. If you have loaded the sessioncomponent and the cookiecomponent in the controller, you can access them as follows: 1 class Postscontroller extends AppController {2 Publ IC $components = Array (' Session ', ' Cookie '); 3 4 Public Function Delete () {5 if ($this->post->delete ($this->request->data (' Post.id ') {6 $this->session->setflash (' Post deleted. '); 7 $this->redirect (' action ' = ' index '); 8}9} Note because the models and components that are joined to the controller as attributes share the same ' namespace ', you need to ensure that the components and the model are not named the same. Load components in run you may not need all of the components to be available in each controller method. In this case, you can use Component Collection to load a component at run time. Inside the controller, you can proceed as follows: 1 $this->onetimer = $this->components->load (' Onetimer '); 2 $this->onetimer-> GetTime (); The component callback component also provides some request lifecycle callbacks to allow them to extend the request period. For more detailed information about the callbacks provided by the component, see Component APIs. Creating components assumes that our on-line applications need to run a complex mathematical operation in their different parts. We can create a component that wraps the shared logic that is used in several different controllers. The first step is the new component files and classes. The file created is/app/controller/component/mathcomponent.php. Its basic structure is as follows: 1 app::uses (' Component ', ' Controller '); 2 class Mathcomponent extends Component {3 Public function Docomplexopera tion ($amount 1, $amount 2) {4 return $amount 1 + $amount 2;5}6} Annotations All components must inherit Component. Otherwise, an exception is thrown. Include a component in a controller once the component is complete, you can use it in the application controller by placing the component name in the $components array of the controller (see the Components section). The controller will automatically provide a new property named after the component, with this propertyWe can access the instance of the component: 1/* Generates a new component variable $this->math2 and a standard $this->session */3 public $components = Array (' Math ', ' Session '); The components in the AppController will be merged with the components in the other controller. Therefore, you do not need to define the same components two times. When you include a component in a controller, you can also define a set of parameters that are passed to the component's constructor. These parameters will then be processed by the component: 1 Public $components = Array (2 ' Math ' = = Array (3 ' precision ' = = 2,4 ' randomgenerator ' = ' srand ' 5) , 6 ' Session ', ' Auth ' 7); This code will include an array of precision and randomgenerator as the second parameter passed to Mathcomponent::__construct (). By convention, any public property passed on a component will also have a value based on this setting. Using other components in components sometimes one component also needs to use other components. In this case, you can include another component in one component-using the ' $components ' variable: 1//app/controller/component/customcomponent.php 2-in the same way as the controller contains the component App::uses (' Component ', ' Controller '); 3 class Customcomponent extends Component {4//The other Component your Component uses 5 public $components = Array (' Exi Sting '); 6 7 Public Function Initialize (Controller $controller) {8 $this->existing->foo (); 9}10 one public function bar () {1 2//... }14}15//app/controller/component/existingcomponent.php17 app::uses (' Component ', ' ContRoller '), class Existingcomponent extends Component {public Function foo () {21//... 22}23} The Component API Class component component base class provides several methods for loading other components through componentcollection delay and for handling public settings. It also provides properties for all component callbacks. Component::__construct (componentcollection $collection, $settings = Array ()) component base class constructor. All $settings that are public properties will also have values that match the values set in Settings. The callback Www.2cto.comComponent::initialize (Controller $controller) Initialize method is called before the controller's Beforefilter method. The Component::startup (Controller $controller) startup method is called after the controller's Beforefilter but before the controller performs the current action processing. Component::beforerender (Controller $controller) The BeforeRender method is called before the Controller renders the view and layout after executing the request action logic. The Component::shutdown (Controller $controller) shutdown method is called before the output is delivered to the browser. Component::beforeredirect (Controller $controller, $url, $status =null, $exit =true) Beforeredirect method after the controller jump method is called, Called before all other method calls. If this method returns false, it will no longer continue to complete the request's turn. $url, $status, and $exit variables have the same meaning for controller methods. You can also return a string as a URL for a turn, or return an associative array with a key ' URL ', the ' status ' and ' exit ' elements of this array are optional.

http://www.bkjia.com/PHPjc/477792.html www.bkjia.com true http://www.bkjia.com/PHPjc/477792.html techarticle A component component is a logical package that is shared across multiple controllers. If people find themselves wanting to copy and paste something between controllers, you should consider wrapping some functionality in a component ...

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