Qiang Xue
11 years ago
16 changed files with 203 additions and 29 deletions
@ -0,0 +1,73 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\web; |
||||
|
||||
use Yii; |
||||
use yii\base\Object; |
||||
|
||||
/** |
||||
* CompositeUrlRule represents a collection of related URL rules. |
||||
* |
||||
* These URL rules are typically created for a common purpose (e.g. RESTful API for a resource). |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
abstract class CompositeUrlRule extends Object implements UrlRuleInterface |
||||
{ |
||||
/** |
||||
* @var UrlRuleInterface[] the URL rules contained in this composite rule. |
||||
* This property is set in [[init()]] by the return value of [[createRules()]]. |
||||
*/ |
||||
protected $rules = []; |
||||
|
||||
|
||||
/** |
||||
* Creates the URL rules that should be contained within this composite rule. |
||||
* @return UrlRuleInterface[] the URL rules |
||||
*/ |
||||
abstract protected function createRules(); |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
$this->rules = $this->createRules(); |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function parseRequest($manager, $request) |
||||
{ |
||||
foreach ($this->rules as $rule) { |
||||
/** @var \yii\web\UrlRule $rule */ |
||||
if (($result = $rule->parseRequest($manager, $request)) !== false) { |
||||
Yii::trace("Request parsed with URL rule: {$rule->name}", __METHOD__); |
||||
return $result; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function createUrl($manager, $route, $params) |
||||
{ |
||||
foreach ($this->rules as $rule) { |
||||
/** @var \yii\web\UrlRule $rule */ |
||||
if (($url = $rule->createUrl($manager, $route, $params)) !== false) { |
||||
return $url; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\web; |
||||
|
||||
/** |
||||
* UrlRuleInterface is the interface that should be implemented URL rule classes. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
interface UrlRuleInterface |
||||
{ |
||||
/** |
||||
* Parses the given request and returns the corresponding route and parameters. |
||||
* @param UrlManager $manager the URL manager |
||||
* @param Request $request the request component |
||||
* @return array|boolean the parsing result. The route and the parameters are returned as an array. |
||||
* If false, it means this rule cannot be used to parse this path info. |
||||
*/ |
||||
public function parseRequest($manager, $request); |
||||
/** |
||||
* Creates a URL according to the given route and parameters. |
||||
* @param UrlManager $manager the URL manager |
||||
* @param string $route the route. It should not have slashes at the beginning or the end. |
||||
* @param array $params the parameters |
||||
* @return string|boolean the created URL, or false if this rule cannot be used for creating this URL. |
||||
*/ |
||||
public function createUrl($manager, $route, $params); |
||||
} |
Loading…
Reference in new issue