CakePHP Component謫堺ス

繧ウ繝ウ繝昴シ繝阪Φ繝医→縺ッ

繧ウ繝ウ繝医Ο繝シ繝ゥ縺ョ讖溯ス繧呈僑蠑オ縺吶k繝励Ο繧ー繝ゥ繝

迢ャ閾ェ繧ウ繝ウ繝昴シ繝阪Φ繝

繧ウ繝ウ繝医Ο繝シ繝ゥ

use App\Controller\Component;

繝ュ繝シ繝
$this->loadComponent('Util');

繧ウ繝ウ繝昴シ繝阪Φ繝医ョ菴ソ逕ィ
$point = $this->Util->getPoint();

繧ウ繝ウ繝昴シ繝阪Φ繝

窶サsrc\Controller\Component\UtilComponent.php
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\ORM\TableRegistry;

class UtilComponent extends Component
{
縲縲public function startup(){
縲縲縲$this->Users = TableRegistry::get('users');
縲縲}

縲縲public function getPoint()
縲縲{
縲縲縲$point = $this->Users
縲縲縲縲->find()
縲縲縲縲->select(['point'])
縲縲縲縲->where(['id' => $_SESSION['Auth']['User']['id']])
縲縲縲縲->hydrate(false)
縲縲縲縲->toList()[0]['point'];
縲縲縲縲
縲縲縲return $point;
縲縲}
}

繝輔Λ繝繧キ繝・繧ウ繝ウ繝昴シ繝阪Φ繝

窶サ繧ウ繝ウ繝医Ο繝シ繝ゥ
$this->loadComponent('Flash');
$this->Flash->set('Test');

窶サ繝薙Η繝シ
<?= $this->Flash->render() ?>

Auth繧ウ繝ウ繝昴シ繝阪Φ繝

繧ウ繝ウ繝医Ο繝シ繝ゥ

Auth繧ウ繝ウ繝昴シ繝阪Φ繝郁ソス蜉蜃ヲ逅
$this->loadComponent(
縲'Auth', [
縲縲'authorize' => ['Controller'],
縲縲'authenticate' => 隱崎ィシ縺ォ髢「縺吶k險ュ螳
縲縲'loginRedirect' => 繝ュ繧ー繧、繝ウ蠕後ョ繝ェ繝繧、繝ャ繧ッ繝亥
縲縲'logoutRedirect' => 繝ュ繧ー繧「繧ヲ繝亥セ後ョ繝ェ繝繧、繝ャ繧ッ繝亥
縲縲'authError' => 隱崎ィシ繧ィ繝ゥ繝シ譎ゅΓ繝繧サ繝シ繧ク
縲]
);

窶サ繧ウ繝ウ繝医Ο繝シ繝ゥ
public function initialize()
{
縲縲parent::initialize();

縲縲$this->loadComponent('RequestHandler');
縲縲$this->loadComponent('Flash');
縲縲$this->loadComponent(
縲縲縲'Auth', [
縲縲縲縲'authorize' => ['Controller'],
縲縲縲縲'authenticate' => [
縲縲縲縲縲'Form' => [
縲縲縲縲縲縲'fields' => [
縲縲縲縲縲縲縲'username' => 'username',
縲縲縲縲縲縲縲'password' => 'password',
縲縲縲縲縲縲]
縲縲縲縲縲]
縲縲縲縲],
縲縲縲縲'loginRedirect' => [
縲縲縲縲縲'controller' => 'Users',
縲縲縲縲縲'action' => 'index',
縲縲縲縲],
縲縲縲縲'logoutRedirect' => [
縲縲縲縲縲'controller' => 'Users',
縲縲縲縲縲'action' => 'login',
縲縲縲縲],
縲縲縲縲'authError' => '繝ュ繧ー繧、繝ウ縺励※縺上□縺輔>縲',
縲縲縲]
縲縲);
}

繝ュ繧ー繧、繝ウ繝壹シ繧ク

<div>
縲<?= $this->Flash->render('auth') ?>
縲<?= $this->Form->create() ?>
縲縲<fieldset>
縲縲縲<legend>繧「繧ォ繧ヲ繝ウ繝亥錐 / 繝代せ繝ッ繝シ繝</legend>
縲縲縲<?= $this->Form->input('username') ?>
縲縲縲<?= $this->Form->input('password') ?>
縲縲</fieldset>
縲縲<?= $this->Form->button(__('騾∽ソ。')); ?>
縲<?= $this->Form->end() ?>
</div>

callback繝。繧ス繝繝峨ョ螳溯。碁蠎

Controller Component
initialize
initialize initialize(array $config)
beforeFilter beforeFilter(Event $event)
beforeFilter
startup startup(Event $event)
action
beforeRender beforeRender(Event $event)
beforeRender
shutdown shutdown(Event $event)
afterFilter

Follow me!

蜑阪ョ險倅コ

CakePHP Contoler謫堺ス

谺。縺ョ險倅コ

CakePHP Behavior謫堺ス