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

Configuring Components
Many of the core components require configuration. Some examples of components requiring configuration
are Authentication, Cookie and EmailComponent. Configuration for these components, and for components
in general, is usually done in the $components array or your controller’s beforeFilter() method:
class PostsController extends AppController {
public $components = array(
’Auth’ => array(
’authorize’ => array(’controller’),
’loginAction’ => array(’controller’ => ’users’, ’action’ => ’login’)
),
’Cookie’ => array(’name’ => ’CookieMonster’)
);
Would be an example of configuring a component with the $components array. All core components
allow their configuration settings to be set in this way. In addition you can configure components in your
controller’s beforeFilter() method. This is useful when you need to assign the results of a function
to a component property. The above could also be expressed as:
public function beforeFilter() {
$this->Auth->authorize = array(’controller’);
$this->Auth->loginAction = array(’controller’ => ’users’, ’action’ => ’login’);
$this->Cookie->name = ’CookieMonster’;
}It’s possible, however, that a component requires certain configuration options to be set before the controller’s
beforeFilter() is run. To this end, some components allow configuration options be set in the
$components array:
public $components = array(
’DebugKit.Toolbar’ => array(’panels’ => array(’history’, ’session’))
);
Consult the relevant documentation to determine what configuration options each component provides.
One common setting to use is the className option, which allows you to alias components. This feature
is useful when you want to replace $this->Auth or another common Component reference with a custom
implementation:
// app/Controller/PostsController.php
class PostsController extends AppController {
public $components = array(
’Auth’ => array(
’className’ => ’MyAuth’
)
);
}
// app/Controller/Component/MyAuthComponent.php
App::uses(’AuthComponent’, ’Controller/Component’);
class MyAuthComponent extends AuthComponent {
// Add your code to override the core AuthComponent
}
The above would alias MyAuthComponent to $this->Auth in your controllers.
Note: Aliasing a component replaces that instance anywhere that component is used, including inside other
Components.
Using Components
Once you’ve included some components in your controller, using them is pretty simple. Each component
you use is exposed as a property on your controller. If you had loaded up the SessionComponent and
the CookieComponent in your controller, you could access them like so:
class PostsController extends AppController {
public $components = array(’Session’, ’Cookie’);
public function delete() {
if ($this->Post->delete($this->request->data(’Post.id’)) {
$this->Session->setFlash(’Post deleted.’);
$this->redirect(array(’action’ => ’index’));
}
}

 
 
 

pankaj

Skills    Cakephp

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

  Students (0)