>  기사  >  백엔드 개발  >  cakephp 구성 요소의 액세스 컨트롤러 인스턴스

cakephp 구성 요소의 액세스 컨트롤러 인스턴스

黄舟
黄舟원래의
2016-12-20 09:57:431149검색

구성 요소의 컨트롤러 인스턴스에 액세스하려면 구성 요소의 초기화() 또는 시작() 메서드를 구현해야 합니다. 이 두 가지 특수 메서드는 첫 번째 인수로 컨트롤러에 대한 참조를 받고 자동으로 호출됩니다. 초기화() 메서드는 컨트롤러의 beforeFilter() 메서드가 실행되기 전에 자동으로 호출되고, start() 메서드는 beforeFilter 메서드가 실행된 후에 자동으로 호출됩니다. 어떤 이유로 컨트롤러가 생성 작업을 수행할 때 start() 메서드가 호출되는 것을 원하지 않는 경우 클래스 멤버 변수 $disableStartup을 true로 설정할 수 있습니다.

컨트롤러의 beforeFilter() 이전에 작업을 수행하려면 초기화() 메서드가 가장 적절한 선택입니다.

class CheckComponent extends Object {
//Controller::beforeFilter()
function 초기화(&$controller) {
// 컨트롤러 저장 전에 호출됨 나중에 사용하기 위한 참조
$this->controller =& $controller;
}

//Controller::beforeFilter()
함수 시작(&$ 컨트롤러) 후에 호출됩니다. {
}

functionredirectSomewhere($value) {
// 컨트롤러 메서드 사용
$this->controller->redirect($value);
}
}
?>

class CheckComponent extends Object {

//In Controller::beforeFilter( )가 <🎜 전에 호출되었습니다. >

함수 초기화(&$controller) {

// 나중에 사용하기 위해 컨트롤러 참조 저장

$this->controller =& $controller

}

//Controller::beforeFilter()

function start(&$controller) {

}

function receiveSomewhere($value) 이후에 호출됩니다. {

//컨트롤러 메소드 사용

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

}

}

?>

때때로 구성 요소에서 다른 구성 요소를 사용해야 할 수도 있습니다. 구성 요소에서 클래스 멤버 변수 $comComponents만 선언하면 됩니다(컨트롤러에서와 마찬가지로). 사용하려는 구성 요소 이름의 배열입니다.

class MyComponent extends Object {

// 사용해야 하는 기타 구성요소
var $comComponents = array('Session', 'Math') ;

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

}
?>

class MyComponent 확장 객체 {

// 사용해야 하는 기타 구성요소

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

function doStuff() {

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

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

}

}

?>

구성 요소에서 모델을 사용하거나 액세스하는 것은 권장되지 않지만 이 경우에는 다음이 필요합니다. 사용할 모델의 인스턴스를 수동으로 생성합니다. 예는 다음과 같습니다.

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

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

class MathComponent 확장 객체 {

function doComplexOperation($amount1, $amount2) {

return $amount1 + $amount2;

}

function doUberComplexOperation($amount1 , $amount2) {

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

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

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

}

}

?>

이 섹션에 대한 댓글 보기

3.6.3.3 구성 요소에 다른 구성 요소 사용

번역

이 섹션만 보기

댓글(0)

역사

아직 이 섹션에 대한 번역이 없습니다. 도움을 주시고 번역해 주세요. 번역에 대한 추가 정보

때때로 구성 요소 중 하나가 다른 구성 요소를 사용해야 할 수도 있습니다.

다른 구성 요소를 포함할 수 있습니다. 구성 요소를 컨트롤러에 포함하는 것과 똑같은 방식으로 구성 요소를 구성합니다. $comComponentsvar.

class CustomComponent extends Object {
var $name = "Custom" // 구성 요소 이름
var $comComponents = array( "Existing" ); // 구성 요소가 사용하는 다른 구성 요소

function 초기화(&$controller) {
$this->Existing ->foo();
}

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

class CustomComponent extends Object {

var $name = "Custom"; // 구성요소 이름

var $comComponents = array( "Existing" ); // 구성요소가 사용하는 다른 구성요소

function initialize(&$controller) {

$this->Existing->foo();

}

function bar() {

// ...

}

}

?>

< ;?php
class ExistingComponent 확장 객체 {
   var $name = "Existing";

   function 초기화(&$controller) {
       $this->Parent->bar( );
   }

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

< ?php

class ExistingComponent extends Object {

var $name = "Existing";

function initialize(&$controller) {

$this- >Parent->bar();

}

function foo() {

// ...

}

}

 以上就是cakephp 组件中访问控器的实例的内容,更多更关内容请关注PHP中文网(www.php.cn)!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.