How to create user friendly clean seo url in yii2 with url manager.

Yii2 url router is a powerful and it helps to make a clean and pretty url.Yii2 URL router is quite powerful and does two main tasks: it resolves URLs into internal routes and creates URLs from these routes. If you want to hide index.php (http://yiiapp.local/index.php?r=site/test) from your url and make it clean like site/test.




'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,// Disable r= routes
    'showScriptName' => false,// Disable index.php
],


URL Rules Manage:

Clean url are help even better indexing for search engine. If that’s not enough consider the fact that you can hide the structure of your app by defining your own rules. ‘enableStrictParsing’ => true . it limits the access only configured rules. In the example given below http://yiiapp.local route will point to site/index, but the http://yiiapp.local/site/index will show a 404 page.For a securite reason you can always use enableStrictParsing .


'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => true,
    'rules' => [
        '/' => 'site/index',
        'login' => 'site/login',
        'logout' => 'site/logout',
    ],
]

Custom URL Rules for configured urls:
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. Here are some examples.


'components' => [
 'urlManager' => [    
  'showScriptName' => false, // Disable index.php
  'enablePrettyUrl' => true, // Disable r= routes
  'enableStrictParsing' => true,
  'rules' => array(
   'categorypage/<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://yii2app.local/login' => 'admin/user/login',
   'http://yii2app.local/login' => 'site/login',
   'http://<country:\w+>.domain.com/profile' => 'user/view',
   '<controller:\w+>/<id:\d+>-<slug:[A-Za-z0-9 -_.]+>' => '<controller>/view',
   ),
 ],
],

First Url: http://yiiapp.local/categorypage/add

In your controller action page like


public function actionAdd()
{
 .........
}


Second Url rule: http://yiiapp.local/mycategory/categorypage/page-one

Third Url rule: http://yiiapp.local/categorypage/index-one

For slug value url:
http://127.0.0.1/yii2/advanced/backend/web/index.php/validation/page-value


//URL Suffixes add like .html, .php
'components' => [
 'urlManager' => [
  'enablePrettyUrl' => true,
  'showScriptName' => false,
  'enableStrictParsing' => true,
  'suffix' => '.html',
  'rules' => [
   // ...
  ],
    ],
],

http://yiiapp.local/categorypage/index.html


Every URL will be process by URL pattern rules. Some time we need to break a rule and override the rule like.


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


Named Parameters Urls:


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


No comments:

Post a Comment

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...

Popular