Custom URL rules in Yii Framework

Try out this tutorial if you want create a news reader app and apply this tutorial 🙂

Custom URL rules

Yii2 give us the opportunity to customize URL rules as we want. This can be done using the rules property in urlManager, an array where keys are patterns and values are corresponding routes. Patterns are common regular expression patterns, so it is necessary to have some familiarity with regular expression.

Patterns can contain parameters that will be passed to the route. In the next example, we will display a list of news that can be filtered through year or category parameter, based on parameters passed to the URL.

Example – list news items by year or category

In this example, we will create a new Controller named News in controllers/NewsController.php. In this new controller, we will insert a data() function containing an array with test data, and a function named actionItemsList.

The first thing to do is to configure the rules property under the urlManager component under config/web.php:

'rules' => [
    news/<year:d{4}>/items-list' => ' news/items-list',
    'news/<category:w+>/items-list' => 'test-rules/items-list',
],

Here, we have two patterns:

  • news/<year:d{4}>/items-list
  • news/<category:w+>/items-list

The first pattern catches requests with a numeric parameter with four digits, passed to the news /items-list route as the year GET parameter. We can request ‘news/2014/items-list’ or ‘news/2015/items-list’.

The second pattern catches requests with the word parameter, passed to the news/items-list route as the category GET parameter. We can request news/business/items-list or news/shopping/items-list.

Then, we create NewsController where to define the data() function, to return static data to be used as data source, and the actionItemsList() function to handle requests to news/year/or/category/itemsList:

<?php

namespace appcontrollers;

use Yii;
use yiiwebController;

class NewsController extends Controller
{
  public function data()
  {
    return [
    [ "id" => 1, "date" => "2015-04-19", "category" => "business", "title" => "Test news of 2015-04-19" ],
    [ "id" => 2, "date" => "2015-05-20", "category" => "shopping", "title" => "Test news of 2015-05-20" ],
    [ "id" => 3, "date" => "2015-06-21", "category" => "business", "title" => "Test news of 2015-06-21" ],
    [ "id" => 4, "date" => "2016-04-19", "category" => "shopping", "title" => "Test news of 2016-04-19" ],
    [ "id" => 5, "date" => "2017-05-19", "category" => "business", "title" => "Test news of 2017-05-19" ],
    [ "id" => 6, "date" => "2018-06-19", "category" => "shopping", "title" => "Test news of 2018-06-19" ]
    ];
  }

  public function actionItemsList()
  {
    // if missing, value will be null
    $year = Yii::$app->request->get('year');
    // if missing, value will be null
    $category = Yii::$app->request->get('category');

    $data = $this->data();
    $filteredData = [];

    foreach($data as $d)
    {
      if(($year != null)&&(date('Y', strtotime($d['date'])) == $year)) $filteredData[] = $d;
      if(($category != null)&&($d['category'] == $category)) $filteredData[] = $d;
    }

    return $this->render('itemsList', ['year' => $year, 'category' => $category, 'filteredData' => $filteredData] );
    }



About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares

Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)