Yii 2 Quick Help Notes

Yii 2

Following basic items good to keep in notes during Yii 2 based web development.

Generating link:

 <?=  Html::a('Home',  ['site/index'],  ['class' => 'class-name'])?>

Generating image link:

$logo = Html::img(\Yii::$app>request->BaseUrl . '/images/logo.png',
['class' => 'img-responsive',
'alt' => 'Logo',
'title' => 'Company logo'

echo Html::a($logo,
['class' => 'class-name']);

Hiding the input field label name:

<?= $form->field($model, 'sample_text')->textArea()->label(false) ?>

Add custom layout in controller function:

$this->layout = 'new-layout';

Work with verb filter:

In controller header, add following:

use yii\filters\VerbFilter;

Inside behavior function add following:

public function behaviors(){
  return [
   'verbs' => [
   'class' => VerbFilter::className(),
   'actions' => [
   //'logout' => ['post'], Note: Here, you can declare verb name for any specific function. But if you already declared in view file using i.e. echo Html::a('Logout',
                 ['site/logout'], ['data-method' => 'post']); then you don't need to mention the verb name again here. If you do then it will display error message.

Add icon before href title name:

<?= Html::a('<i class="glyphicon glyphicon-dashboard d-icon"></i>' . ' Admin Dashboard', ['site/admin-dashboard']) ?>

Render any view file from  view:

<?= Yii::$app->view->renderFile('@app/views/site/_admin-view.php'); ?>

Exit mobile version