djidji/yii2-default-url-rule Parsing and creating url with arguments pretified as well instead of in query string format

UrlRule for default basic logic in parsing and creating pretty Url.

  1. Conventions
  2. Installation
  3. Usage
  4. Examples

Parsing and creating url with arguments pretified as well instead of in query string format. Route and argument segments of url are separated by the UrlManager->routeParam


  1. Argument is prettified only if its name is declared as action method parameter otherwise it stay in query string format;

  2. Arguments are orderer by the way corresponding action method parameters are declared;

  3. Optional Argument nth binds to its corresponding action method parameter only if all arguments --nth are explicitly passed. --nth argments are inserted at their respective places in created url if were nor explicit;

  4. Arguments less than required or more than action method parameters show 404


The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist djidji/yii2-default-url-rule "*"

or add

"djidji/yii2-default-url-rule": "*"

to the require section of your composer.json file.


Once the extension is installed, simply use it in your code by declaring in rules of Urlmanager :

'rules' => [
    // ...other url rules...
        'class' => 'djidji\DefaultUrlRule',
        // complete file path to save routes.
        'routesFile' => '@app/config/routes.php'


class PostController extends Controller
    public function actionIndex($category,$year=2015,$tag='')

    public function actionView($id)

To create Url
  • Url::to(['post/index', 'year' => 2014, 'category' => 'php']) creates /index.php/post/index/r/php/2014 ;

  • Url::to(['post/index', 'category' => 'php']) creates /index.php/post/index/r/php;

  • Url::to(['post/index', 'category' => 'php','tag'=>'programming']) creates /index.php/post/index/r/php/2015/programming. default value of parameter 'year' is inserted at its place;

  • Url::to(['post/index','year' => 2014]) result to false because the argument for required $category parameter is not passed;

  • Url::to(['post/view', 'id' => 100]) creates /index.php/post/view/r/100 ;

  • Url::to(['post/view', 'id' => 100, 'source' => 'ad']) creates creates /index.php/post/view/r/100?source=ad. Because "source" argument is not declared as actionView methoth, it is appended as a query parameter in the created URL.

To parse Url
  • /index.php/post/index result to false because actionIndex has required firs parameter that need argments to be passed.

  • /index.php/post/index/r/php is parsed to ['post/index', ['category' => 'php']] ;

  • /index.php/post/index/r/php/2015/programming is parsed to ['post/index', ['category' => 'php','tag'=>'programming','year'=>2015]] ;

  • /index.php/post/index/r/php/programming is parsed to ['post/index', ['category' => 'php','year'=>'programming']] ;

  • /index.php/post/view/r/100?source=ad is parsed to ['post/view', ['id' => 100]];

  • /index.php/post/view/r/100/ad?source=ad result to false because actionView method expect one argment instead of two.

0 0
1 follower
Yii Version: 2.0
License: BSD-3-Clause
Category: Web Service
Developed by: djidji01
Created on: Apr 27, 2020
Last updated: (not set)
Packagist Profile
Github Repository

Related Extensions