How to configure URL rules in YII framework

Dream. Dare. Do – that is Suyati’s work principle in a nutshell.

Nov
20
2013
  • Author:
  • Arun Balakrishnan

Introduction

YII URL router is quite powerful and does two main tasks: it resolves URLs into internal routes and creates URLs from these routes. Let’s try to understand how to configure application rules by simple example.

Let’s begin

As a first step: Create a fresh Yii application and find your protected/config/main.php. It should contain the following:

// application components
'components'=>array(
...
// uncomment the following to enable URLs in path-format
/*
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:w+>/<id:d+>'=>'<controller>/view',
'<controller:w+>/<action:w+>/<id:
d+>'=>'<controller>/<action>',
'<controller:w+>/<action:w+>'=>'<controller>/<action>',
),
),

Now delete everything from rules as we are going to do from scratch.

Go to  your protected/controllers, create SiteController.php with the following code :

<?php

class SiteController extends CController
{
public function actionIndex()
{
echo "index";
}
public function actionPage($alias)
{
echo "This is $alias.";
}
}

This is the application controller we are going to customize URLs for.

Let’s configure our application server to use clean URLs. If you are using Apache with mod_rewrite and AllowOverride turned on, then add the following lines into your  .htaccess file under the root folder:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# else forward it to index.php
RewriteRule . index.php

How to do this?

Our website should display the index page at /home and all other pages at /page/<alias_here>. Additionally, /about should lead to a page with alias about.

Now add the below to your rules in protected/config/main.php:
'home' => 'site/index',
'<alias:about>' => 'site/page',
'page/<alias>' => 'site/page',

Save the above changes and browse the following URLs:

  • ‰‰ /home
  • ‰‰ /about
  • ‰‰ /page/about
  • ‰‰ /page/test

When /about URL is used: You can see the following in your browser:

This is about.

What is happening!

Let’s review what we did and how it works.  Have a look at the first rule:
'home' => 'site/index',

What is site/index exactly?

In the Yii framework, a format for an internal route is moduleID/controllerID/actionID. For example, the actionPage method of SiteController corresponds to the site/page route. So, the controller ID should be its name without the Controller postfix and make its first letter lowercased. To get an action ID, you should take action method name without the action prefix and, again, make its first letter lowercased.

Now, what is home?

To have a better understanding, we need to know, at least perfunctorily, what’s happening when we access our application using different URLs.

When we are using /home, URL router checks our rules one by one starting from the top trying to match URL entered with the rule. If the match is found, then the router is getting controller and its action from an internal route assigned to the rule and is executing it. So, /home is the URL pattern that defines which URLs will be processed by the rule it belongs to.

More Rules…

You can create parameterized rules using a special syntax. Let’s take the third one:
'page/<alias>' => 'site/page',
Here, we are defining an alias parameter that should be specified in URL after /page/. It can be virtually anything and it will be passed as $alias parameter to
SiteController::actionPage($alias).

You can define a pattern for such a parameter:
'<alias:about>' => 'site/page',
Alias here should match about or else, the rule will not be applied.

Read More

Visit the following URLs to get more information and examples related to this topic.

Leave a Comment

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