2010-05-16 51 views

回答

13

試圖使控制器正確輸出它們。讓我知道他們中的任何一個是否關閉。

class Controller_Info extends Controller 
{ 
    public function action_index() 
    { 
     $uris = array 
     (
      'page' => array 
      (
       'a' => Request::instance()->uri(), 
       'b' => URL::base(TRUE, FALSE).Request::instance()->uri(), 
       'c' => URL::site(Request::instance()->uri()), 
       'd' => URL::site(Request::instance()->uri(), TRUE), 
      ), 

      'application' => array 
      (
       'a' => URL::base(), 
       'b' => URL::base(TRUE, TRUE), 
       'c' => URL::site(), 
       'd' => URL::site(NULL, TRUE), 
      ), 
     ); 

     $this->request->headers['Content-Type'] = 'text/plain'; 
     $this->request->response = print_r($uris, true); 
    } 

    public function action_version() 
    { 
     $this->request->response = 'Kohana version: '.Kohana::VERSION; 
    } 

    public function action_php() 
    { 
     phpinfo(); 
    } 

} 

輸出這樣的:

Array 
(
    [page] => Array 
     (
      [a] => info/index 
      [b] => /kohana/info/index 
      [c] => /kohana/info/index 
      [d] => http://localhost/kohana/info/index 
     ) 
    [application] => Array 
     (
      [a] => /kohana/ 
      [b] => http://localhost/kohana/ 
      [c] => /kohana/ 
      [d] => http://localhost/kohana/ 
     ) 
) 

從技術上講,它實際上僅在第一頁URL,它是一個真正的相對URL,因爲所有的人要麼/http://啓動。


需要自己獲取當前頁面的url,所以決定擴展url類。以爲我可以在這裏分享。讓我知道你在想什麼:)

/** 
* Extension of the Kohana URL helper class. 
*/ 
class URL extends Kohana_URL 
{ 
    /** 
    * Fetches the URL to the current request uri. 
    * 
    * @param bool make absolute url 
    * @param bool add protocol and domain (ignored if relative url) 
    * @return string 
    */ 
    public static function current($absolute = FALSE, $protocol = FALSE) 
    { 
     $url = Request::instance()->uri(); 

     if($absolute === TRUE) 
      $url = self::site($url, $protocol); 

     return $url; 
    } 
} 

echo URL::current();   // controller/action 
echo URL::current(TRUE);  // /base_url/controller/action 
echo URL::current(TRUE, TRUE); // http://domain/base_url/controller/action 
+2

3.1+你需要傳遞一個字符串(「HTTP ','https')或Request對象添加到URL :: site中的$ protocol參數。如果你想保留查詢字符串,你可以在最後添加URL :: query()。 – Enrique 2012-01-07 21:01:37

7

你不只是說: Kohana_Request :: detect_uri()?

+0

乾杯oncejr。使用Request :: detect_uri()是完美的。 – 2011-06-06 07:30:57

2

絕對/相對當前網址:

// outputs 'http://www.example.com/subdir/controller/action' 
echo URL::site(Request::detect_uri(),true)); 

// outputs '/subdir/controller/action' 
echo URL::site(Request::detect_uri()); 

絕對/相對當前的應用程序URL:

// outputs 'http://www.example.com/subdir/' 
echo URL::site(NULL, TRUE); 

// outputs '/subdir/' 
echo URL::site(); 

希望它可以幫助在Kohana的