问题描述:

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 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.

相关阅读:
Top