解析网址以获得Kohana 3中的控制器和操作

时间:2021-12-04 20:14:51

I'm having problem to detect the controller/action name of the referrer page in Kohana 3.

我在Kohana 3中检测引用者页面的控制器/动作名称时遇到问题。

What I have to do is to detect whether the referrer page is from internal or external.If it is external (e.g. from google), I will do some default setting. If it is internal referrer (i.e. from the same domain), I need to do something different according to the controller and action information of that referrer page.

我要做的是检测引荐来源页面是来自内部还是外部。如果它是外部的(例如来自谷歌),我会做一些默认设置。如果它是内部引用者(即来自同一域),我需要根据该引用者页面的控制器和动作信息做一些不同的事情。

I start with checking the $_SERVER["HTTP_REFERRER"], but I stuck at getting the controller and action name from that variable. Since I have customized routes in bootstrap, I want to get the same

我开始检查$ _SERVER [“HTTP_REFERRER”],但我坚持从该变量获取控制器和操作名称。由于我在bootstrap中定制了路由,所以我希望得到相同的结果

I know Kohana provides methods to get the controller and action of current request.

我知道Kohana提供了获取当前请求的控制器和动作的方法。

$this->request->controller
$this->request->action
$this->request->param('paramname')

While we wonder if there are the methods that can parse a given URL string and return the controller/action/parameters information.

虽然我们想知道是否有方法可以解析给定的URL字符串并返回控制器/动作/参数信息。

Any ideas??

有任何想法吗??

UPDATE:

更新:

After hours of study in Kohana source code, I found a solution that is in 2 steps:

在Kohana源代码中学习了几个小时后,我发现了一个分为两个步骤的解决方案:

Step 1. convert the URL to URI. If it is from external referrer, the URI should be NULL.

步骤1.将URL转换为URI。如果它来自外部引用者,则URI应为NULL。

function URL2URI($URL)
{
    if (empty($URL)) return NULL;
    $url_info = parse_url($URL);
    if (!isset($url_info['host']) || !isset($url_info['path'])) return NULL;
    return ($url_info['host'] === $_SERVER['HTTP_HOST']) ? ltrim($url_info['path'], '/') : NULL;
}

Step 2. Test the URI with all routes and get the info from the route that matches the URI ($match['controller'], $match['action']).

步骤2.使用所有路由测试URI并从匹配URI的路由获取信息($ match ['controller'],$ match ['action'])。

function getInfoFromURI($URI)
{
     if (empty($URI)) return NULL;    
     $routes = Route::all();
     foreach ($routes as $oneRoute)
     if ($match = $oneRoute->matches($URI))
          return $match;    
     return NULL;
 }

1 个解决方案

#1


1  

Shouldn't you use:

你不应该使用:

$controller = Request::factory($your_url_without_http)->controller;
$action = Request::factory($your_url_without_http)->action;

#1


1  

Shouldn't you use:

你不应该使用:

$controller = Request::factory($your_url_without_http)->controller;
$action = Request::factory($your_url_without_http)->action;