- Yii Tutorial
- Yii - Home
- Yii - Overview
- Yii - Installation
- Yii - Create Page
- Yii - Application Structure
- Yii - Entry Scripts
- Yii - Controllers
- Yii - Using Controllers
- Yii - Using Actions
- Yii - Models
- Yii - Widgets
- Yii - Modules
- Yii - Views
- Yii - Layouts
- Yii - Assets
- Yii - Asset Conversion
- Yii - Extensions
- Yii - Creating Extensions
- Yii - HTTP Requests
- Yii - Responses
- Yii - URL Formats
- Yii - URL Routing
- Yii - Rules of URL
- Yii - HTML Forms
- Yii - Validation
- Yii - Ad Hoc Validation
- Yii - AJAX Validation
- Yii - Sessions
- Yii - Using Flash Data
- Yii - Cookies
- Yii - Using Cookies
- Yii - Files Upload
- Yii - Formatting
- Yii - Pagination
- Yii - Sorting
- Yii - Properties
- Yii - Data Providers
- Yii - Data Widgets
- Yii - ListView Widget
- Yii - GridView Widget
- Yii - Events
- Yii - Creating Event
- Yii - Behaviors
- Yii - Creating a Behavior
- Yii - Configurations
- Yii - Dependency Injection
- Yii - Database Access
- Yii - Data Access Objects
- Yii - Query Builder
- Yii - Active Record
- Yii - Database Migration
- Yii - Theming
- Yii - RESTful APIs
- Yii - RESTful APIs in Action
- Yii - Fields
- Yii - Testing
- Yii - Caching
- Yii - Fragment Caching
- Yii - Aliases
- Yii - Logging
- Yii - Error Handling
- Yii - Authentication
- Yii - Authorization
- Yii - Localization
- Yii - Gii
- Gii – Creating a Model
- Gii – Generating Controller
- Gii – Generating Module
- Yii Useful Resources
- Yii - Quick Guide
- Yii - Useful Resources
- Yii - Discussion
Yii - Rules of URL
A URL rule is an instance if yii\web\UrlRule. The urlManager components uses the URL rules declared in its rules property when the pretty URL format is enabled.
To parse a request, the URL manager obtains the rules in the order they are declared and looks for the first rule.
Step 1 − Modify the urlManager component in the config/web.php file.
'urlManager' => [ 'showScriptName' => false, 'enablePrettyUrl' => true, 'rules' => [ 'about' => 'site/about', ] ],
Step 2 − Go to your web browser at http://localhost:8080/about, you will see the about page.
A URL rule can be associated with query parameters in this pattern −
<ParamName:RegExp>, where −
ParamName − The parameter name
RegExp − An optional regular expression used to match parameter values
Suppose, we have declared the following URL rules −
[ 'articles/<year:\d{4}>/<category>' => 'article/index', 'articles' => 'article/index', 'article/<id:\d+>' => 'article/view', ]
When the rules are used for parsing −
- /index.php/articles is parsed into the article/index
- /index.php/articles/2014/php is parsed into the article/index
- /index.php/article/100 is parsed into the article/view
- /index.php/articles/php is parsed into articles/php
When the rules are used for creating URLs −
Url::to(['article/index']) creates /index.php/articles
Url::to(['article/index', 'year' => 2014, 'category' => 'php']) creates /index.php/articles/2014/php
Url::to(['article/view', 'id' => 100]) creates /index.php/article/100
Url::to(['article/view', 'id' => 100, 'source' => 'ad']) creates /index.php/article/100?source=ad
Url::to(['article/index', 'category' => 'php']) creates /index.php/article/index?category=php
To add a suffix to the URL, you should configure the yii\web\UrlManager::$suffix property.
Step 3 − Modify the urlComponent in the config/web.php file.
'urlManager' => [ 'showScriptName' => false, 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'suffix' => '.html' ],
Step 4 − Type the address http://localhost:8080/site/contact.html in the address bar of the web browser and you will see the following on your screen. Notice the html suffix.
To Continue Learning Please Login
Login with Google