SoFunction
Updated on 2025-04-04

Analysis of routing configuration method of Yii framework

This article describes the routing configuration method of Yii framework. Share it for your reference, as follows:

Cancel

Both methods are added automatically

Method 1: Use .htaccess

Add .htaccess file with the same level

RewriteEngine on
# if a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to 
RewriteRule . 

Method 2: vhost

<VirtualHost *:80>
    ServerName 
    DocumentRoot "D:\phpStudy\PHPTutorial\WWW\OA\frontend\web"
    <Directory "D:\phpStudy\PHPTutorial\WWW\OA\frontend\web">
      # use mod_rewrite for pretty URL support
      RewriteEngine on
      # If a directory or a file exists, use the request directly
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      # Otherwise forward the request to 
      RewriteRule . 
      # use  as index file
      DirectoryIndex 
      # ...other settings...
      # Apache 2.4
      Require all granted
      ## Apache 2.2
      # Order allow,deny
      # Allow from all
    </Directory>
</VirtualHost>

Yii configuration

    'urlManager' =&gt; [
      //Beautify the route      'enablePrettyUrl' =&gt; true,
      //Don't enable strict parsing      'enableStrictParsing' =&gt; false,
      // Whether to display      'showScriptName' =&gt; false,
      //Pseudostatic seo      'suffix' =&gt; '.html',
      //Beautify rules      'rules' =&gt; [
        //Article 1: Article details page        '&lt;controller:\w+&gt;/&lt;id:\d+&gt;'=&gt;'&lt;controller&gt;/detail',
        //Article 2: Article list page        'post'=&gt;'post/index',
      ],
    ],

For more information about Yii, readers who are interested in this site can view the topic:Yii framework introduction and common techniques summary》、《Summary of excellent development framework for php》、《Basic tutorial on getting started with smarty templates》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Yii framework.