cakephp3

CakePHP3勉強会 in Fusic ハンズオン資料 part.2

More than 1 year has passed since last update.

前. CakePHP3勉強会 in Fusic ハンズオン資料 part.1
http://qiita.com/Junkins/items/f1f830b33e52dc1cbc38

3. Auth

ログイン画面の作成

参考URL

http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html

  • src/Controller/AppController.php の修正
command
    vim src/Controller/AppController.php
vim
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'userModel' => 'Accounts',
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginRedirect' => [
                'controller' => 'Accounts',
                'action' => 'index'
            ],
            'loginAction' => [
                'controller' => 'Accounts',
                'action' => 'login',
            ],
        ]);
    }
  • src/Controller/AccountsController.php の修正
command
    vim src/Controller/AccountsController.php
vim
    public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }

    public function logout()
    {
        return $this->redirect($this->Auth->logout());
    }
  • src/Template/Accounts/login.ctp の作成
command
    vim src/Template/Accounts/login.ctp
vim
    <!-- File: src/Template/Users/login.ctp -->
    <div class="users form">
        <?= $this->Flash->render('auth') ?>
        <?= $this->Form->create() ?>
            <fieldset>
                <legend><?= __('Please enter your username and password') ?></legend>
                <?= $this->Form->input('username') ?>
                <?= $this->Form->input('password') ?>
            </fieldset>
        <?= $this->Form->button(__('Login')); ?>
        <?= $this->Form->end() ?>
    </div>
  • src/Model/Entity/Account.php の修正
command
    vim src/Model/Entity/Account.php 
vim
    use Cake\Auth\DefaultPasswordHasher;
vim
    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }

4. Query Builder

Query Builderで遊ぼう

参考URL

http://book.cakephp.org/3.0/ja/orm/query-builder.html

  • Query Builder デバック
comannd
    vim src/Controller/AccountsController.php
vim
    public function debug()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        debug($query);
    }
  • サンプル
vim
    $this->autoRender = false;
    $query = $this->Accounts->find();
    $query->where([$this->Accounts->alias() . '.name' => 'AccountA']);
    debug($query);
    // SQLの実行
    debug($query->firstOrFail());
vim
    public function sql1()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        $query->where([$this->Accounts->alias() . '.name' => 'AccountA']);
        debug($query);
        // SQLの実行
        debug($query->firstOrFail());
    }
vim
    public function sql2()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        $query->matching('Projects', function ($q) {
            return $q->where(['Projects.name' => 'ProjectB']);
        });

        debug($query);
        // SQLの実行
        debug($query->all());
    }
vim
    public function sql3()
    {
        $this->autoRender = false;
        $query = $this->Accounts->find();
        $query->matching(
            'Projects.Tasks', function ($q) {
                return $q->where(['Tasks.name' => 'TaskE']);
            }
        );

        debug($query);
        // SQLの実行
        debug($query->all());
    }

前. CakePHP3勉強会 in Fusic ハンズオン資料 part.1
http://qiita.com/Junkins/items/f1f830b33e52dc1cbc38