PHP code example of blumster / yii2-model-based-controller

1. Go to this page and download the library: Download blumster/yii2-model-based-controller 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/ */

    

blumster / yii2-model-based-controller example snippets


protected function handleError($error)
{
    return false;
}

$this->redirect([ 'somwhere/nice' ]);
return false;

Url::to([ 'company/someaction', 'id' => <company id> ])

ModelUrl::to([ 'company/someaction' ]);

use blumster\controller\BaseModelController;

class CompanyController extends BaseModelController
{
    /**
     * @var \app\models\Company|null
     */
    protected $model = null; // Override the BaseModelController::$model, so an IDE (for example: PhpStorm) can make use of the @var <type> to use the auto-completion feature, this line is only for the auto-completion. It is optional.

    /**
     * @var string
     */
    protected $paramName = 'cid'; // defaults to 'id' in the BaseModelController
    
    /**
     * @var string
     */
    protected $idColumnName = 'main_id'; // default to 'id' in the BaseModelController
    
    /**
     * @inheritdoc
     */
    protected static function  can specify extra conditions, based on the action. You must use BaseModelController::RELATION_JOIN_WITH to use the relation's table in the where condition
        }
        
        return $query;
    }
    
    /**
     * Magic index action.
     */
    public function actionIndex()
    {
        return $this->render('index', [
            'model' => $this->model,
            'relation1Entries' => $this->model->relation1 // Already loaded (by i::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;
                return ActiveForm::validate($model);
            }

            if ($model->save()) {
                return $this->redirect([ 'company/list' ]);
            } else {
                Yii::error('Unable to save Company! Errors: ' . print_r($model->getErrors(), true));
            }
        }

        return $this->render('form', [
            'model' => $model
        ]);
    }
}