Yii Framework 2 : URL Manager

Each and every application will be accessed by URL. We can configure the URL format and can define the ‘RULES’ How to show and access the page via easy and pretty URL. Yii2.0 is having a flexibility to format the URL using urlManager components and we have to define it in web.php file under ‘config’ folder. Please see the following code

Default URL

In Yiiframework 2.0, By default, The default controller is ‘site’ and action is ‘index’. We will call this like ‘site/index’. If we want, we can customize this using ‘defaultRoute’ parameter in web.php.


//........
$config = [
    'defaultRoute' => '/tblcategory/index',
    'components' => [
	'urlManager' => [
            .....
	    'rules' => array(
	        .....
	    ),
	],
    ],
];
........

Pretty URL

To show the pretty URL to customer, Just enable the ‘enablePrettyUrl’ as true in ‘urlManager’ component of yiiframwork 2.0.


........
$config = [
    'components' => [
        'urlManager' => [
	    'showScriptName' => false,	// Disable index.php
	    'enablePrettyUrl' => true,	// Disable r= routes
	    'enableStrictParsing' => true,
	    'rules' => array(
	        .....
	    ),
	],
    ],
];
........

Configure URL Rules

yii\web\UrlRule is the URL rule class of yii2. Each and every url will be considered as pattern and accessed by this pattern rules. We added the url rules below.

URL Manager Rules Configuration in web.php

//........
'components' => [
    'urlManager' => [
        'showScriptName' => false,	// Disable index.php
	'enablePrettyUrl' => true,	// Disable r= routes
	'enableStrictParsing' => true,
	'rules' => array(
	    'mycategory/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
	    '<controller:\w+>/<id:\d+>' => '<controller>/view',
	    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
	    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
	    //Rules with Server Names
	    'http://admin.domain.com/login' => 'admin/user/login',
	    'http://www.domain.com/login' => 'site/login',
	    'http://<country:\w+>.domain.com/profile' => 'user/view',
	    '<controller:\w+>/<id:\d+>-<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
	),
    ],
],
........
URL Format:1
http://localhost/project/web/tblcategory/create

public function actionCreate()
{
    .........
}
URL Format:2
http://localhost/project/web/mycategory/tblcategory/index-one

public function actionCreate()
{
    .........
}
URL Format:3
http://localhost/project/web/tblcategory/index-one

public function actionIndexOne()
{
    .........
}
URL Format:4
http://127.0.0.1/yii2/advanced/backend/web/index.php/validation/4-SLUG-VALUE

echo Html::a("View", ['view', 'id' => 4, 'slug' => "SLUG-VALUE"]);

URL Suffixes

To make url pretty more good, Just add the suffix to the url like .html, .json etc.


'components' => [
    'urlManager' => [
	'enablePrettyUrl' => true,
	'showScriptName' => false,
	'enableStrictParsing' => true,
	'suffix' => '.html',
	'rules' => [
            // ...
	],
    ],
],
http://localhost/project/web/tblcategory/index.html

Customizing Rules

Every URL will be process by URL patter rules. But some time we want to break the rules and have to override with new rules. For this, we will set the new rule inside the ‘rules’ array of ‘urlManager’.


[
    // ...other url rules...
    'suffix' => '.html',
    'rules' => array(
        [
	    'pattern' => 'site/test',
	    'route' => 'site/contact',
	    'suffix' => '.json',
	],
	[
	    'pattern' => 'site/api',
	    'route' => 'site/about',
	    'suffix' => '.json',
	],
	'<controller:\w+>/<id:\d+>' => '<controller>/view',
	'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
	'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    )
]

By default URL link for ‘Contact’ page

http://localhost/project/web/site/contact.html

After the pattern url configured

http://localhost/project/web/site/test.json

Named Parameters

We can set the parameter name based on requesting URL in specified format.


[
    'posts/<year:\d{4}>/<category>' => 'post/index',
    'posts' => 'post/index',
    'post/<id:\d+>' => 'post/view',
]

HTTP Methods

To Access the controller and actions based on HTTP methods, We have to use PUT, POST, DELETE etc method before the url patter format like below code


[
    'PUT,POST post/<id:\d+>' => 'post/create',
    'DELETE post/<id:\d+>' => 'post/delete',
    'post/<id:\d+>' => 'post/view',
]

 

Leave a Reply

Your email address will not be published. Required fields are marked *