title: parse url to get controller and action in Kohana 3
body: <p>I'm having problem to detect the controller/action name of the referrer page in Kohana 3.</p>
  
  <p>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. </p>
  
  <p>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</p>
  
  <p>I know Kohana provides methods to get the controller and action of current request.</p>
  
  <pre><code>$this-&gt;request-&gt;controller
  $this-&gt;request-&gt;action
  $this-&gt;request-&gt;param('paramname')
  </code></pre>
  
  <p>While we wonder if there are the methods that can parse a given URL string and return the controller/action/parameters information. </p>
  
  <p>Any ideas??</p>
  
  <p>UPDATE:</p>
  
  <p>After hours of study in Kohana source code, I found a solution that is in 2 steps:</p>
  
  <p>Step 1. convert the URL to URI. If it is from external referrer, the URI should be NULL.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>Step 2. Test the URI with all routes and get the info from the route that matches the URI ($match['controller'], $match['action']).</p>
  
  <pre><code>function getInfoFromURI($URI)
  {
       if (empty($URI)) return NULL;    
       $routes = Route::all();
       foreach ($routes as $oneRoute)
       if ($match = $oneRoute-&gt;matches($URI))
            return $match;    
       return NULL;
   }
  </code></pre>