Configuring URL rules
In this recipe, we will learn how to configure URL rules. Before we begin lets set up an application.
Getting ready
Create a new application using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
Create the
@app/controllers/TestController.php
controller with the following code inside:<?php namespace app\controllers; use yii\helpers\Html; use yii\web\Controller; class TestController extends Controller { public function actionIndex() { return $this->renderContent(Html::tag('h2', 'Index action' )); } public function actionPage($alias) { return $this->renderContent(Html::tag('h2', 'Page is '. Html::encode($alias) )); } }
This is the application controller that we are going to customize URLs for.
Configure your application server to use clean URLs. If you are using Apache with
mod_rewrite
andAllowOverride...