Вы могли помочь мне со следующими вопросами. Как делают я добираюсь:
абсолютный/относительный текущий URL
абсолютный/относительный URL приложения
Я мог, конечно, использовать собственный php для получения его, но я думаю, что должен скорее использовать функции ko3.
Какая-либо идея, как это работает?
Заранее спасибо!
Пытался сделать контроллер, который бы все выводил правильно. Сообщите мне, если какие-то из них отключены.
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