O'Reilly logo

Web Application Development with Yii 2 and PHP by Jeffrey Winesett, Mark Safronov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Custom routes using a configuration

Look at the following path we go to when opening the View Customer Record page:

/customer-records/view?id=1

This is quite verbose. Why not just use the following:

/customer/1

We can implement it quite easily using the following declaration inside the components.urlManager.rules setting of the Yii application configuration:

'components' => [
    'urlManager' => [
        'rules' => [
            'customer/<id:\d+>' => 'customer-records/view',
        ]
    ]
]

This declaration is being read and understood as follows:

  1. If the request begins with customer/.
  2. After this there are only digits.
  3. Store these digits as an argument named id.
  4. Process the request as the customer-records/view route.
  5. Pass the stored id argument to the resulting controller action.

FEATURE ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required