Cakephp Classroom image

pankaj
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26|Last
Lessons:-Controllers

Note: Since both Models and Components are added to Controllers as properties they share the same
‘namespace’. Be sure to not give a component and a model the same name.
Loading components on the fly
You might not need all of your components available on every controller action. In situations like this you
can load a component at runtime using the Component Collection. From inside a controller you can do the
following:
$this->OneTimer = $this->Components->load(’OneTimer’);
$this->OneTimer->getTime();
Component Callbacks
Components also offer a few request life-cycle callbacks that allow them to augment the request cycle. See
the base Component API for more information on the callbacks components offer.
Creating a Component
Suppose our online application needs to perform a complex mathematical operation in many different parts
of the application. We could create a component to house this shared logic for use in many different controllers.
The first step is to create a new component file and class. Create the file in
/app/Controller/Component/MathComponent.php. The basic structure for the component
would look something like this:
App::uses(’Component’, ’Controller’);
class MathComponent extends Component {
public function doComplexOperation($amount1, $amount2) {
return $amount1 + $amount2;
}
}
Note: All components must extend Component. Failing to do this will trigger an exception.
Including your component in your controllers
Once our component is finished, we can use it in the application’s controllers by placing the component’s
name (minus the “Component” part) in the controller’s $components array. The controller will automatically
be given a new attribute named after the component, through which we can access an instance of
it:

 
 
 

pankaj

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)