开发者

parse url to get controller and action in Kohana 3

开发者 https://www.devze.com 2023-02-05 04:56 出处:网络
I\'m having problem to detect the controller/action name of the referrer page in 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

I'm having problem to detect the controller/action name of the referrer page in 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

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

$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.

Any ideas??

UPDATE:

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

Step 1. convert the URL to URI. If it is from external referrer, the URI should be 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']).

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


Shouldn't you use:

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

精彩评论

暂无评论...
验证码 换一张
取 消