开发者

Kohana 3 - How should I handle urls within views

I'm running Kohana 3 (latest) and I don't know how to handle links in my views. For example. My base url is http://localhost/foo/. I have a menu like and this menu is shared between bar and baz page:

<a href="bar">Bar</a>
<a开发者_Go百科 href="baz">Baz</a>

If i'm in my home page, my links are:

http://localhost/foo/bar
http://localhost/foo/baz.

These links are in the same. If i click into bar link, for example the my links will be something like:

http://localhost/foo/bar/bar
http://localhost/foo/bar/baz.

What's wrong? How should i handle urls? Thanks.


The simplest way would be to use:

URL::site('bar');

This will make a relative URL, including the base_path and index.php (if it's enabled, ofc).

You can also use Route::url() to produce the same thing using a route.

Route::url('route_name', array('id' => $id));

This doesn't concern views only but all sitelinks.


Also you can use Kohana's Html::anchor() method:

echo HTML::anchor('bar', 'Bar');


You could also just include <base> tag in your <head>, something like this:

<head>
  ...
  <base href="<?php echo url::base(); ?>" />
  ...
</head>

With this inside your view, every link you use will be relative to <base> not to the current address. This way <a href="bar">Bar</a> will always be pointing to http://localhost/foo/bar no matter on which page you use it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜