The concept of URL management in Yii fairly simple. URL management is based on the premise that the application uses internal routes and parameters
everywhere. The framework itself will then translates routes into URLs, and translate URLs into routes, according to the URL manager's configuration.
This approach allows you to change site-wide URLs merely by editing a single config file, without ever touching the application code.
The concept of URL management in Yii fairly simple. URL management is based on the premise that the application uses
internal routes and parameters everywhere. The framework itself will then translates routes into URLs, and translate URLs
into routes, according to the URL manager's configuration. This approach allows you to change site-wide URLs merely by
editing a single config file, without ever touching the application code.
Internal route
--------------
When implementing an application using Yii, you'll deal with internal routes and parameters. Each controller and controller action has a corresponding internal route, such as `site/index` or `user/create`. In the former, `site` is referred to as the *controller ID* while `index` is referred to as the *action ID*. In the second example, `user` is the controller ID and `create` is the action ID. If controller belongs to a *module*, the internal route is prefixed with the module ID, such as `blog/post/index` for a blog module (with `post` being the controller ID and `index` being the action ID).
When implementing an application using Yii, you'll deal with internal routes, often referred to as routes, and parameters.
Each controller and controller action has a corresponding internal route, such as `site/index` or `user/create`.
In the former, `site` is referred to as the *controller ID* while `index` is referred to as the *action ID*. In the
second example, `user` is the controller ID and `create` is the action ID. If controller belongs to a *module*, the
internal route is prefixed with the module ID, such as `blog/post/index` for a blog module (with `post` being the
controller ID and `index` being the action ID).
Creating URLs
-------------
...
...
@@ -20,8 +26,10 @@ application component with the `urlManager` ID. This component is accessible bot
-`createUrl($route, $params = [])`
-`createAbsoluteUrl($route, $params = [])`
The `createUrl` method creates a URL relative to the application root, such as `/index.php/site/index/`. The `createAbsoluteUrl` method creates URL prefixed with the proper protocol and
hostname: `http://www.example.com/index.php/site/index`. The former is suitable for internal application URLs, while the latter is used when you need to create rules for outside the website, such as when sending emails or generating an RSS feed.
The `createUrl` method creates a URL relative to the application root, such as `/index.php/site/index/`.
The `createAbsoluteUrl` method creates URL prefixed with the proper protocol and hostname:
`http://www.example.com/index.php/site/index`. The former is suitable for internal application URLs, while the latter
is used when you need to create rules for outside the website, such as when sending emails or generating an RSS feed.
Inside a web application controller, you can use the controller's own `createUrl` shortcut method. Unlike the global `createUrl` method, the controller version is context sensitive:
Inside a web application controller, you can use the controller's own `createUrl` shortcut method. Unlike the global
`createUrl` method, the controller version is context sensitive:
```php
echo$this->createUrl('');// currently active route
...
...
@@ -61,7 +71,10 @@ Customizing URLs
----------------
By default, Yii uses a query string format for URLs, such as `/index.php?r=news/view&id=100`. In order to make URLs
human-friendly (i.e., more readable), you need to configure the `urlManager` component in the application's configuration file. Enabling "pretty" URLs will convert the query string format to a directory-based format: `/index.php/news/view/id/100`. Disabling the `showScriptName` parameter means that `index.php` will not be part of the URLs. Here's the relevant part of the application's configuration file.
human-friendly (i.e., more readable), you need to configure the `urlManager` component in the application's configuration
file. Enabling "pretty" URLs will convert the query string format to a directory-based format: `/index.php/news/view?id=100`.
Disabling the `showScriptName` parameter means that `index.php` will not be part of the URLs. Here's the relevant part of
the application's configuration file:
```php
<?php
...
...
@@ -76,11 +89,100 @@ return [
];
```
Note that this configuration will only work if the web server has been properly configured for Yii, see [installation](installation.md#recommended-apache-configuration).
Note that this configuration will only work if the web server has been properly configured for Yii, see