Yii Tutorial on Yii URL Routing

to change the default route of the application, you should configure the defaultroute property.

step 1 − modify the config/web.php file in the following way.

<?php
   $params = require(__dir__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basepath' => dirname(__dir__),
      'bootstrap' => ['log'],
      'defaultroute' => 'site/contact',
      'components' => [
         //other code
?>

step 2 − got to http://localhost:8080/index.php. you will see the default contact page.

contact page

to put your application in maintenance mode temporarily, you should configure the yii\web\application::$catchall property.

step 3 − add the following function to the sitecontroller.

public function actionmaintenance() {
   echo "<h1>maintenance</h1>";
}

step 4 − then, modify the config/web.php file in the following way.

<?php
   $params = require(__dir__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basepath' => dirname(__dir__),
      'bootstrap' => ['log'],
      'catchall' => ['site/maintenance'],
      'components' => [
         //other code

step 5 − now enter any url of your application, you will see the following.

maintenance

creating urls

to create various kinds of urls you may use the yii\helpers\url::to() helper method. the following example assumes the default url format is being used.

step 1 − add an actionroutes() method to the sitecontroller.

public function actionroutes() {
   return $this->render('routes');
}

this method simply renders the routes view.

step 2 − inside the views/site directory, create a file called routes.php with the following code.

<?php
   use yii\helpers\url;
?>

<h4>
   <b>url::to(['post/index']):</b>
   <?php
      // creates a url to a route: /index.php?r = post/index
      echo url::to(['post/index']);
   ?>
</h4>

<h4>
   <b>url::to(['post/view', 'id' => 100]):</b>
   <?php
      // creates a url to a route with parameters: /index.php?r = post/view&id=100
      echo url::to(['post/view', 'id' => 100]);
   ?>
</h4>

<h4>
   <b>url::to(['post/view', 'id' => 100, '#' => 'content']):</b>
   <?php
      // creates an anchored url: /index.php?r = post/view&id=100#content
      echo url::to(['post/view', 'id' => 100, '#' => 'content']);
   ?>
</h4>

<h4>
   <b>url::to(['post/index'], true):</b>
   <?php
      // creates an absolute url: http://www.example.com/index.php?r=post/index
      echo url::to(['post/index'], true);
   ?>
</h4>

<h4>
   <b>url::to(['post/index'], 'https'):</b>
   <?php
      // creates an absolute url using the https scheme: https://www.example.com/index.php?r=post/index
      echo url::to(['post/index'], 'https');
   ?>
</h4>

step 3 − type http://localhost:8080/index.php?r=site/routes, you will see some uses of the to() function.

to function

the route passed to the yii\helpers\url::to() method can be relative or absolute according to the following rules −

  • if the route is empty, the currently requested route will be used.

  • if the route has no leading slash, it is considered to be a route relative to the current module.

  • if the route contains no slashes, it is considered to be an action id of the current controller.

the yii\helpers\url helper class also provides several useful methods.

step 4 − modify the routes view as given in the following code.

<?php
   use yii\helpers\url;
?>

<h4>
   <b>url::home():</b>
   <?php
      // home page url: /index.php?r=site/index
      echo url::home();
   ?>
</h4>
 
<h4>
   <b>url::base():</b>
   <?php
      // the base url, useful if the application is deployed in a sub-folder of the web root
      echo url::base();
   ?>
</h4>
 
<h4>
   <b>url::canonical():</b>
   <?php
      // the canonical url of the currently requested url
      // see https://en.wikipedia.org/wiki/canonical_link_element
      echo url::canonical();
   ?>
</h4>
 
<h4>
   <b>url::previous():</b>
   <?php
      // remember the currently requested url and retrieve it back in later requests
      url::remember();
      echo url::previous();
   ?>
</h4>

step 5 − if you enter the address http://localhost:8080/index.php?r=site/routes in the web browser, you will see the following.

modified routes view outputs