2010-05-16 19 views
7

क्या आप निम्नलिखित प्रश्नों के साथ मेरी मदद कर सकते हैं। मैं कैसे मिलता है:कोहाना 3 - यूआरएल प्राप्त करें

निरपेक्ष/सापेक्ष वर्तमान यूआरएल

निरपेक्ष/सापेक्ष आवेदन यूआरएल

मैं निश्चित रूप से इसे पाने के लिए देशी php इस्तेमाल कर सकते हैं लेकिन मुझे लगता है कि मैं नहीं बल्कि ko3 कार्यों का उपयोग करना चाहिए।

कोई विचार यह कैसे काम करता है?

अग्रिम धन्यवाद!

उत्तर

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/ 
     ) 
) 

तकनीकी रूप से, यह वास्तव में केवल प्रथम पृष्ठ यूआरएल कोई वास्तविक रिश्तेदार यूआरएल है कि, के बाद से सभी दूसरों को भी / या http:// साथ शुरू करते हैं।


वर्तमान पृष्ठ के लिए यूआरएल प्राप्त करने की आवश्यकता है, इसलिए यूआरएल वर्ग का विस्तार करने का फैसला किया गया। सोचा था कि मैं इसे यहां साझा कर सकता हूं। Kohana_Request :: detect_uri(): मुझे पता है आप क्या सोचते हैं :)

/** 
* 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 ') या यूआरएल :: साइट में $ प्रोटोकॉल पैरामीटर के लिए अनुरोध ऑब्जेक्ट। और यदि आप क्वेरी स्ट्रिंग को संरक्षित करना चाहते हैं तो आप अंत में URL :: query() जोड़ सकते हैं। – Enrique

7

क्या आप सिर्फ मतलब यह नहीं है?

+0

चीयर्स दो बार। अनुरोध का उपयोग :: detect_uri() सही है। –

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()); 

निरपेक्ष/सापेक्ष वर्तमान आवेदन यूआरएल:

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

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

यह मदद करता है Kohana पर

संबंधित मुद्दे