Yii 2 Quick Help Notes

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’,
‘title’=>’Company logo’

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

Hiding 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’); ?>


……more coming……

How to configure SEO friendly PrettyUrl in Yii2?

You can easily configure your Yii2 web applications URL SEO friendly by following bellow steps-

Step 1: Add urlManager in config/web.php

Step 2: Add .htaccess in web/ directory

Step 3: Check virtual host setup in httpd-vhosts.conf file


Step 1: Add urlManager in config/web.php

Add following code in config/web.php ‘components’ section and save the file. For Yii2 advanced setup please go to \common\config\main.php file and add in components’ section.

'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,

Step 2: Add .htaccess in web/ directory

Create .htaccess file inside web/ directory with following text-

RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php


Step 3: Check virtual host setup in httpd-vhosts.conf file

Go to extra directory i.e. C:\wamp\bin\apache\apache2.4.9\conf\extra and then open httpd-vhosts.conf. Check AllowOverride value setup as all settings inside <VirtualHost *:80> </VirtualHost>.

DocumentRoot “c:/wamp/www/mysite”
ServerName mysite.dev
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from


Check and confirm the file before closing. Now restart Apache server and check web url i.e. mysite.dev/site/about

Enjoy exploring Yii2.