Browse Source

Fixed URLManager bugs.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
a5fe4a6af2
  1. 4
      framework/web/UrlManager.php
  2. 7
      framework/web/UrlRule.php
  3. 18
      tests/unit/framework/web/UrlManagerTest.php

4
framework/web/UrlManager.php

@ -156,7 +156,7 @@ class UrlManager extends Component
public function createUrl($route, $params = array())
{
$anchor = isset($params['#']) ? '#' . $params['#'] : '';
unset($anchor['#']);
unset($params['#']);
$route = trim($route, '/');
$baseUrl = $this->getBaseUrl();
@ -165,7 +165,7 @@ class UrlManager extends Component
/** @var $rule UrlRule */
foreach ($this->rules as $rule) {
if (($url = $rule->createUrl($this, $route, $params)) !== false) {
return rtrim($baseUrl, '/') . $url . $anchor;
return rtrim($baseUrl, '/') . '/' . $url . $anchor;
}
}

7
framework/web/UrlRule.php

@ -10,6 +10,7 @@
namespace yii\web;
use yii\base\Object;
use yii\base\InvalidConfigException;
/**
* UrlRule represents a rule used for parsing and generating URLs.
@ -86,6 +87,12 @@ class UrlRule extends Object
*/
public function init()
{
if ($this->pattern === null) {
throw new InvalidConfigException('UrlRule::pattern must be set.');
}
if ($this->route === null) {
throw new InvalidConfigException('UrlRule::route must be set.');
}
if ($this->verb !== null) {
if (is_array($this->verb)) {
foreach ($this->verb as $i => $verb) {

18
tests/unit/framework/web/UrlManagerTest.php

@ -1,12 +1,14 @@
<?php
namespace yiiunit\framework\web;
use yii\web\Application;
use yii\web\UrlManager;
class UrlManagerTest extends \yiiunit\TestCase
{
public function testCreateUrl()
{
new Application('test', __DIR__ . '/../..');
// default setting with '/' as base url
$manager = new UrlManager(array(
'baseUrl' => '/',
@ -14,14 +16,14 @@ class UrlManagerTest extends \yiiunit\TestCase
$url = $manager->createUrl('post/view');
$this->assertEquals('/?r=post/view', $url);
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/?r=post/view&id=1&title=sample%20post', $url);
$this->assertEquals('/?r=post/view&id=1&title=sample+post', $url);
// default setting with '/test/' as base url
$manager = new UrlManager(array(
'baseUrl' => '/test/',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/?r=post/view&id=1&title=sample%20post', $url);
$this->assertEquals('/test/?r=post/view&id=1&title=sample+post', $url);
// pretty URL without rules
$manager = new UrlManager(array(
@ -29,19 +31,19 @@ class UrlManagerTest extends \yiiunit\TestCase
'baseUrl' => '/',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/view?r=post/view&id=1&title=sample%20post', $url);
$this->assertEquals('/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/test/',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/post/view?r=post/view&id=1&title=sample%20post', $url);
$this->assertEquals('/test/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/test/index.php',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/index.php/post/view?r=post/view&id=1&title=sample%20post', $url);
$this->assertEquals('/test/index.php/post/view?id=1&title=sample+post', $url);
// todo: test showScriptName
@ -51,12 +53,13 @@ class UrlManagerTest extends \yiiunit\TestCase
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
),
),
'baseUrl' => '/',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/view/1/sample%20test', $url);
$this->assertEquals('/post/1/sample+post', $url);
$url = $manager->createUrl('post/index', array('page' => 1));
$this->assertEquals('/post/index?page=1', $url);
@ -66,13 +69,14 @@ class UrlManagerTest extends \yiiunit\TestCase
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
),
),
'baseUrl' => '/',
'suffix' => '.html',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/view/1/sample%20test.html', $url);
$this->assertEquals('/post/1/sample+post.html', $url);
$url = $manager->createUrl('post/index', array('page' => 1));
$this->assertEquals('/post/index.html?page=1', $url);
}

Loading…
Cancel
Save