Instance of access controller in cakephp component

黄舟
Release: 2023-03-03 16:36:01
Original
1218 people have browsed it

If you want to access the instance of the controller in a component, you need to implement the initialize() or startup() method of the component. These two special methods receive a reference to the controller as the first argument and are called automatically. The initialize() method is automatically called before the controller's beforeFilter() method is executed, and the startup() method is automatically called after the beforeFilter method is executed. If for some reason you don't want the startup() method to be called when the controller performs a construction operation, you can set the class member variable $disableStartup to true.

If you want to do something before beforeFilter() of the controller, the initialize() method is the most appropriate choice.

class CheckComponent extends Object {
//Called before Controller::beforeFilter()
function initialize(&$controller) {
// saving the controller reference for later use
$this-> controller =& $controller;
}

//Called after Controller::beforeFilter()
function startup(&$controller) {
}

function redirectSomewhere($value) {
// Method to use the controller
$this->controller->redirect($value);
}
}
?>

class CheckComponent extends Object {

//Before Controller::beforeFilter() Called

function initialize(&$controller) {

// saving the controller reference for later use

$this->controller =& $controller;

}

//in Controller::beforeFilter() Afterwards it is called

function startup(&$controller) {

}

function redirectSomewhere($value) {

// Method of using the controller

$this->controller->redirect($value) ;

}

}

?>

Sometimes you may need to use other components in the component, you only need to declare the class member variable $components in the component (just like in the controller), Its value is an array of component names you want to use.

class MyComponent extends Object {

// Other components that need to be used
var $components = array('Session', 'Math');

function doStuff() {
$result = $this ->Math->doComplexOperation(1, 2);
$this->Session->write('stuff', $result);
}

}
?>

class MyComponent extends Object {

// Other components that need to be used

var $components = array('Session', 'Math');

function doStuff() {

$result = $this-> Math->doComplexOperation(1, 2);

$this->Session->write('stuff', $result);

}

}

?>

Use in component or Accessing the model is not recommended; however, it is possible, in which case you need to manually generate an instance of the model for use. Here is an example:

class MathComponent extends Object {
function doComplexOperation($amount1, $amount2) {
return $amount1 + $amount2;
}

function doUberComplexOperation ($amount1, $amount2) {
$userInstance = ClassRegistry::init('User');
$totalUsers = $userInstance->find('count');
return ($amount1 + $amount2) / $totalUsers;
}
}
?> ;

class MathComponent extends Object {

function doComplexOperation($amount1, $amount2) {

return $amount1 + $amount2;

}

function doUberComplexOperation ($amount1, $amount2) {

$userInstance = ClassRegistry::init('User');

$totalUsers = $userInstance->find('count');

return ($amount1 + $amount2) / $totalUsers;

}

}

?>

See comments for this section

3.6.3.3 Using other Components in your Component

Translate

View just this section

Comments (0)

History

There is no translation yet for this section. Please help out and translate this.. More information about translations

Sometimes one of your components may need to use another.

You can include other components in your component the exact same way you include them in controllers: Use the$componentsvar.

class CustomComponent extends Object {
var $name = "Custom"; // the name of your component
var $components = array( "Existing" ); // the other component your component uses

function initialize(&$controller) {
$this->Existing->foo();
}

function bar() {
// ...
}
}
?>

class CustomComponent extends Object {

var $name = "Custom"; // the name of your component

var $components = array( "Existing" ); // the other component your component uses

function initialize(& $controller) {

$this->Existing->foo();

}

function bar() {

// ...

}

}

?>

< ;?php
class ExistingComponent extends Object {
var $name = "Existing";

function initialize(&$controller) {
$this->Parent->bar();
}

function foo() {
// ...
}}
? & Gt;

& lt;? PHP

Class ExistingComponent Extends Object {

var $ name = "existing";

Function Initialize {{

$this->Parent->bar();

}

function foo() {

// ...

}

}

The above is the instance of accessing the controller in the cakephp component Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template