Commit 6a1816a9 by Alexander Makarov

Allow using string for URL in case there are no parameters

parent 43c17d99
......@@ -37,7 +37,7 @@ echo \Yii::$app->urlManager->createUrl(['site/page', 'id' => 'about']);
// /index.php/site/page/id/about/
echo \Yii::$app->urlManager->createUrl(['date-time/fast-forward', 'id' => 105])
// /index.php?r=date-time/fast-forward&id=105
echo \Yii::$app->urlManager->createAbsoluteUrl(['blog/post/index']);
echo \Yii::$app->urlManager->createAbsoluteUrl('blog/post/index');
// http://www.example.com/index.php/blog/post/index/
```
......@@ -57,9 +57,9 @@ Inside a web application controller, you can use the controller's `createUrl` sh
```php
echo $this->createUrl(''); // currently active route
echo $this->createUrl(['view', 'id' => 'contact']); // same controller, different action
echo $this->createUrl(['post/index']); // same module, different controller and action
echo $this->createUrl(['/site/index']); // absolute route no matter what controller is making this call
echo $this->createurl(['hi-tech']); // url for the case sensitive action `actionHiTech` of the current controller
echo $this->createUrl('post/index'); // same module, different controller and action
echo $this->createUrl('/site/index'); // absolute route no matter what controller is making this call
echo $this->createurl('hi-tech'); // url for the case sensitive action `actionHiTech` of the current controller
echo $this->createurl(['/date-time/fast-forward', 'id' => 105]); // url for action the case sensitive controller, `DateTimeController::actionFastForward`
```
......@@ -114,11 +114,11 @@ Let's use some examples to explain how URL rules work. We assume that our rule s
]
```
- Calling `$this->createUrl(['post/list'])` generates `/index.php/posts`. The first rule is applied.
- Calling `$this->createUrl('post/list')` generates `/index.php/posts`. The first rule is applied.
- Calling `$this->createUrl(['post/read', 'id' => 100])` generates `/index.php/post/100`. The second rule is applied.
- Calling `$this->createUrl(['post/read', 'year' => 2008, 'title' => 'a sample post'])` generates
`/index.php/post/2008/a%20sample%20post`. The third rule is applied.
- Calling `$this->createUrl(['post/read'])` generates `/index.php/post/read`. None of the rules is applied, convention is used
- Calling `$this->createUrl('post/read')` generates `/index.php/post/read`. None of the rules is applied, convention is used
instead.
In summary, when using `createUrl` to generate a URL, the route and the `GET` parameters passed to the method are used to
......
......@@ -110,8 +110,9 @@ class DefaultController extends Controller
/**
* @inheritdoc
*/
public function createUrl(array $params)
public function createUrl($params)
{
$params = (array)$params;
if (!isset($params['id']) && $this->generator !== null) {
foreach ($this->module->generators as $id => $generator) {
if ($generator === $this->generator) {
......
......@@ -149,9 +149,9 @@ class Generator extends \yii\gii\Generator
{
$actions = $this->getActionIDs();
if (in_array('index', $actions)) {
$route = [$this->controller . '/index'];
$route = $this->controller . '/index';
} else {
$route = [$this->controller . '/' . reset($actions)];
$route = $this->controller . '/' . reset($actions);
}
$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($route), ['target' => '_blank']);
return "The controller has been generated successfully. You may $link.";
......
......@@ -84,7 +84,7 @@ class Generator extends \yii\gii\Generator
public function successMessage()
{
if (Yii::$app->hasModule($this->moduleID)) {
$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl([$this->moduleID]), ['target' => '_blank']);
$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), ['target' => '_blank']);
return "The module has been generated successfully. You may $link.";
}
......
......@@ -159,11 +159,12 @@ class Controller extends \yii\base\Controller
*
* After this route conversion, the method calls [[UrlManager::createUrl()]] to create a URL.
*
* @param array $params route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string|array $params route as a string or route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @return string the created relative URL
*/
public function createUrl(array $params)
public function createUrl($params)
{
$params = (array)$params;
$params[0] = $this->getNormalizedRoute($params[0]);
return Yii::$app->getUrlManager()->createUrl($params);
}
......@@ -182,13 +183,14 @@ class Controller extends \yii\base\Controller
*
* After this route conversion, the method calls [[UrlManager::createUrl()]] to create a URL.
*
* @param array $params route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string|array $params route as a string or route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string $schema the schema to use for the url. e.g. 'http' or 'https'. If not specified
* the schema of the current request will be used.
* @return string the created absolute URL
*/
public function createAbsoluteUrl(array $params, $schema = null)
public function createAbsoluteUrl($params, $schema = null)
{
$params = (array)$params;
$params[0] = $this->getNormalizedRoute($params[0]);
return Yii::$app->getUrlManager()->createAbsoluteUrl($params, $schema);
}
......
......@@ -228,11 +228,12 @@ class UrlManager extends Component
/**
* Creates a URL using the given route and parameters.
* The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL.
* @param array $params route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string|array $params route as a string or route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @return string the created URL
*/
public function createUrl(array $params)
public function createUrl($params)
{
$params = (array)$params;
$anchor = isset($params['#']) ? '#' . $params['#'] : '';
unset($params['#'], $params[$this->routeParam]);
......@@ -275,14 +276,15 @@ class UrlManager extends Component
/**
* Creates an absolute URL using the given route and parameters.
* This method prepends the URL created by [[createUrl()]] with the [[hostInfo]].
* @param array $params route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string|array $params route as a string or route and parameters in form of ['route', 'param1' => 'value1', 'param2' => 'value2']
* @param string $schema the schema to use for the url. e.g. 'http' or 'https'. If not specified
* the schema of the current request will be used.
* @return string the created URL
* @see createUrl()
*/
public function createAbsoluteUrl(array $params, $schema = null)
public function createAbsoluteUrl($params, $schema = null)
{
$params = (array)$params;
$url = $this->createUrl($params);
if (strpos($url, '://') === false) {
$url = $this->getHostInfo($schema) . $url;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment