Home Backend Development PHP Tutorial Instance of access controller in cakephp component

Instance of access controller in cakephp component

Dec 20, 2016 am 09:57 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to properly calibrate your Xbox One controller on Windows 11 How to properly calibrate your Xbox One controller on Windows 11 Sep 21, 2023 pm 09:09 PM

Since Windows has become the gaming platform of choice, it's even more important to identify its gaming-oriented features. One of them is the ability to calibrate an Xbox One controller on Windows 11. With built-in manual calibration, you can get rid of drift, random movement, or performance issues and effectively align the X, Y, and Z axes. If the available options don't work, you can always use a third-party Xbox One controller calibration tool. Let’s find out! How do I calibrate my Xbox controller on Windows 11? Before proceeding, make sure you connect your controller to your computer and update your Xbox One controller's drivers. While you're at it, also install any available firmware updates. 1. Use Wind

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How does CakePHP handle file uploads? How does CakePHP handle file uploads? Jun 04, 2023 pm 07:21 PM

CakePHP is an open source web application framework built on the PHP language that simplifies the development process of web applications. In CakePHP, processing file uploads is a common requirement. Whether it is uploading avatars, pictures or documents, the corresponding functions need to be implemented in the program. This article will introduce how to handle file uploads in CakePHP and some precautions. Processing uploaded files in Controller In CakePHP, uploaded files are usually processed in Cont

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

See all articles