CakePHP Contoler謫堺ス

繧「繧ッ繧キ繝ァ繝ウ

繝繝輔か繝ォ繝医い繧ッ繧キ繝ァ繝ウ

<?php
namespace App\Controller;
AppController縺ョ蜷榊燕遨コ髢薙→蜷後§蝣エ謇縺ォ驟咲スョ縺吶k

use App\Controller\AppController;
邯呎価縺ョ轤コ縺ョAppController繧、繝ウ繝昴シ繝

class HelloController extends AppController {
AppController繧堤カ呎価縺吶k

繝繝輔か繝ォ繝医壹シ繧ク蜃ヲ逅繧「繧ッ繧キ繝ァ繝ウ(シ昴Γ繧ス繝繝)
縲public function index(){
縲縲$this->name = 'Hello';
縲縲$this->autoRender = false;
縲縲echo "hello world!"
縲}
}
?>

竊薙ョURL縺ァ蜻シ縺ウ蜃コ縺輔l繧
http://ス/hello/index
http://ス/hello/
http://ス/hello

繧「繧ッ繧キ繝ァ繝ウ


class HelloController extends AppController {
縲ス
test繧「繧ッ繧キ繝ァ繝ウ
縲public function test(){
縲縲echo "test"
縲}
}

竊薙ョURL縺ァ蜻シ縺ウ蜃コ縺輔l繧
http://ス/hello/test

callback繝。繧ス繝繝

Controller
initialize initialize()
beforeFilter beforeFilter(Event $event)
action
beforeRender beforeRender($view = null, $layout = null)
afterFilter beforeFilter()

Component繧貞性繧√◆callback繝。繧ス繝繝峨ョ螳溯。碁蠎上↓縺、縺縺ヲ縺ッ縲callback繝。繧ス繝繝峨ョ螳溯。碁蠎縲榊盾辣ァ

beforeFilter


use Cake\Event\Event;
beforeFilter蠑墓焚シ哘vent繧、繝ウ繝昴シ繝

class HelloController extends AppController {

縲function beforeFilter(Event $event){
縲縲parent::beforeFilter($event);
縲縲ス
縲}

縲public function index(){
縲縲echo "hello world!"
縲}
}

initialize


class HelloController extends AppController {
縲public function initialize(){
縲縲parent::initialize();
縲縲ス
縲}
}

beforeRender


class HelloController extends AppController {
縲public function beforeRender($view = null, $layout = null){
縲縲parent::beforeRender($view, $layout);
縲縲ス
縲}
}

繝壹シ繧ク驕キ遘サ

繝ェ繝繧、繝ャ繧ッ繝

$this->redirect(array(
縲'controller' => 'my_pages',
縲'action' => 'receive'
縲)
);

$this->redirect('http://example/orders/confirm');

$this->redirect('/test/test');

繝輔か繝ッ繝シ繝

$this->setAction("(繧「繧ッ繧キ繝ァ繝ウ蜷)");
萓具シ$this->setAction("test");
繝悶Λ繧ヲ繧カ縺ョURL縺ッ驕キ遘サ蜑

縺昴ョ莉

AJAX

繧ッ繝ゥ繧、繧「繝ウ繝亥ヲ逅縺ッ縲jQuery Ajax縲榊盾辣ァ

public function test($id = null){
縲if($this->request->is('ajax')){
縲縲$ret = ['count' => $id,];
縲縲JSON蠖「蠑上〒霑斐☆
縲縲echo( json_encode($ret) );
縲}
縲return;
}

Follow me!

蜑阪ョ險倅コ

CakePHP Element謫堺ス

谺。縺ョ險倅コ

CakePHP Component謫堺ス