2013-03-12 76 views
1

我搜索了,但没有找到一些东西。 所以,我的路由规则:Yii框架 - 从URL到路由

... 

'/reg' => '/user/user/registration', 
... 

Yii::app()->request 

我不能找到任何路由信息。

所以,我怎样才能在模块初始化函数,并具有唯一的网址,路线LILE

/reg -> user/user/registration 

UPD

+0

显示所有航线:them depe发现他们的订单。 – sensorario 2013-03-12 21:57:37

+0

这是错误的答案。我发现这样做的唯一方法是重写getPathInfo(),getRequestUri(),protected $ _requestUri;和$ _pathInfo;在子类中: ''' class HttpRequest extends CHttpRequest { \t protected $ _requestUri; \t protected $ _pathInfo; \t public function setUri($ uri){ \t \t $ this - > _ requestUri = $ uri; \t} \t公共函数setPathInfo($路线){ \t \t $这 - > _ PATHINFO = $路线; \t} \t公共职能getPathInfo(){/ *从 \t公共职能getRequestUri(){/ *从母公司* /} ''' – alexglue 2015-05-21 11:55:31

+0

副本可你在解释你的答案母公司* /}复制“回答问题“模式@alexglue? – Miroff 2015-06-23 13:45:08

回答

0

今天(我的问题后的第二天),我可以解决这个问题。

我会尽量解释:

正如迈克尔写道,我们不能在模块中的哪个控制器,我们知道。

但是我的网络只是反转路线,所以,它相当好。

Yii::app()->getUrlManager()->parseUrl('/reg'); 

这将返回我的反向航线

user/user/registration 

parseUrl

0

路由只能从正在运行的控制器。当模块初始化时,控制器尚不可用,因此您无法找到该路线。 (您可以按照CWebApplication::processRequest查看当请求解析到控制器运行时发生的情况。)

这取决于您尝试实现的目标,但您可以覆盖WebModule::beforeControllerAction以在模块控制器之前执行某些操作运行。

+0

很抱歉,我更正了我的问题 – Miroff 2013-03-12 14:01:42

+0

现在我明白了。答案已更新。 – 2013-03-12 14:24:01

+0

我找到了答案! – Miroff 2013-03-13 05:18:58

0

解决方案的Yii 1.1.15 workes我。

class HttpRequest extends CHttpRequest { 

    protected $_requestUri; 
    protected $_pathInfo; 

    public function setUri($uri){ 
     $this->_requestUri = $uri; 
    } 

    public function setPathInfo($route){ 
     $this->_pathInfo = $route; 
    } 

    public function getPathInfo(){ 
     /* copy from parent */ 
    } 

    public function getRequestUri(){ 
     /* copy from parent */ 
    } 
} 

用法:

$uri_path = 'my/project-alias/wall'; 

    /** @var HttpRequest $request */ 
    $request = clone Yii::app()->getRequest(); 
    $request->setUri($uri_path); 
    $request->setPathInfo(null); 

    $route = Yii::app()->getUrlManager()->parseUrl($request); 

    //$route equals 'project/profile/wall' etc here (like in route rules); 
+0

我认为,这个解决方案更难。为什么你不喜欢我找到的解决方案? (它工作正常) – Miroff 2015-06-25 08:42:25

+0

@Miroff导致它不适用于Yii 1.1.5:我得到了“致命错误:调用成员函数getPathInfo()在/ vendor/yiisoft/yii/framework/web /第364行的CUrlManager.php“在字符串''$ rawPathInfo = $ request-> getPathInfo();'''因为$ request应该是一个具有设计保护的$ pathInfo等的对象。 – alexglue 2015-06-25 11:36:23

+0

哦谢谢。我测试了它。 – Miroff 2015-12-02 21:59:47

0

我使用了一个稍微不同的子类CHttpRequest的:

class CustomHttpRequest extends \CHttpRequest 
{ 
    /** 
    * @var string 
    */ 
    var $pathInfo; 
    /** 
    * @var string 
    */ 
    private $method; 

    public function __construct($pathInfo, $method) 
    { 
     $this->pathInfo = $pathInfo; 
     $this->method = $method; 
    } 

    public function getPathInfo() 
    { 
     return $this->pathInfo; // Return our path info rather than the default 
    } 

    public function getRequestType() 
    { 
     return $this->method; 
    } 
} 

然后调用它(创建一个控制器,这是什么我想要):

$request = new CustomHttpRequest($uri, $method); // e.g. 'my/project-alias/wall' and 'GET' 
$route = \Yii::app()->getUrlManager()->parseUrl($request); 
list($jcontroller, $actionName) = \Yii::app()->createController($route); 
+0

这个解决方案适用于yii2吗? – Miroff 2015-12-02 22:00:05