PHP code example of satthi / post-transition

1. Go to this page and download the library: Download satthi/post-transition library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

satthi / post-transition example snippets



namespace App\Controller;

use App\Controller\AppController;
use PostTransition\Controller\PostTransitionControllerTrait;

class TopicsController extends AppController
{
    use PostTransitionControllerTrait;
    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
    }
    
    public function add()
    {
        $settings = [
            //次へボタンのprefix(default はnext)
            //'nextPrefix' => 'next',
            //前へボタンのprefix(default はback)
            //'backPrefix' => 'back',
            //現在の状態をセッションで管理するキー(defaultはnow)
            //'nowField' => 'now',
            //扱うモデル
            'model' => 'Topics',
            //初期遷移時の設定
            'default' => [
                //初期値
                //'value' => [],
                //初期画面の設定はpost配列内のどの設定か
                'post_setting' => 'index1',
            ],
            //privateの第二引数に特定の値を引き渡す
            'param' => [],
            //post時の設定
            'post' => [
                //key値は、ボタン名や独自メソッドへのアクセスに使用
                'index1' => [
                    //使用するview
                    'render' => 'index1',
                    //バリデーションをかける際のentity作成第二引数の設定
                    'validate_option' => ['validate' => 'index1']
                ],
                'index2' => [
                    'render' => 'index2',
                    'validate_option' => ['validate' => 'index2']
                ],
                'index3' => [
                    'render' => 'index3',
                    'validate_option' => ['validate' => 'index3']
                ],
                'save' => [
                    'render' => false,
                    'validate_option' => ['validate' => false]
                ],
            ],
        ];
        $this->__postTransition($settings);
    }
    
    //index1の画面描画前(entityセット直前でフック)
    private function __index1($entity, $param){
       $entity->hoge4 = 'are';
    }
    
    //save処理
    private function __save($entity, $param){
        debug($entity);
        //save 処理
    }
}

index1<br />
 //__index1にてentityにセットした値が入る

index2<br />
hoge1:<?= $entity->hoge1;

index3<br />
hoge1:<?= $entity->hoge1;


namespace App\Controller;

use App\Controller\AppController;
use App\Form\TopicForm;
use Cake\ORM\TableRegistry;
use PostTransition\Controller\PostTransitionFormControllerTrait;

class TopicsController extends AppController
{
    use PostTransitionFormControllerTrait;

        public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    }

    public function add()
    {
        $topicForm = new TopicForm();
        $settings = [
            //次へボタンのprefix(default はnext)
            //'nextPrefix' => 'next',
            //前へボタンのprefix(default はback)
            //'backPrefix' => 'back',
            //現在の状態をセッションで管理するキー(defaultはnow)
            //'nowField' => 'now',
            //扱うモデル
            'model' => $topicForm,
            //初期遷移時の設定
            'default' => [
                //初期値
                'value' => [
                    // 'hoge1' => 'hoge'
                ],
                //初期画面の設定はpost配列内のどの設定か
                'post_setting' => 'index1',
            ],
            'param' => 'hogehoge',
            //post時の設定
            'post' => [
                //key値は、ボタン名や独自メソッドへのアクセスに使用
                'index1' => [
                    //使用するview
                    'render' => 'index1',
                    //バリデーションをかける際のentity作成第二引数の設定
                    'validate_option' => ['validate' => 'index1']
                ],
                'index2' => [
                    'render' => 'index2',
                    'validate_option' => ['validate' => 'index2']
                ],
                'index3' => [
                    'render' => 'index3',
                    'validate_option' => ['validate' => 'index3']
                ],
                'save' => [
                    'render' => false,
                    'validate_option' => ['validate' => false]
                ],
            ],
        ];
        $this->__postTransition($settings);
    }

    //index1の画面描画前(entityセット直前でフック)
    private function __index1($data, $param){
        var_dump($data);
        var_dump($param);
    }

    //save処理
    private function __save($data, $param){
        $this->Topics = TableRegistry::get('Topics');
        debug($this->Topics->newEntity($data));
        //save 処理
    }
}




namespace App\Form;

use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;

class TopicForm extends Form
{
    //ここでバリデーションを記述
    protected function _buildValidator(Validator $validator)
    {
        // $validator
        //     ->notEmpty('hoge1', '->notEmpty('hoge2', 'そう
}


index1<br />
<?= $this->Form->create($contactForm);

index2<br />
hoge1:<?= $this->request->data['hoge1'];

index3<br />
hoge1:<?= $this->request->data['hoge1'];