4

Example I have the following code in Controller:

class Main extends CI_Controller {

    public function index()
    {
        $this->load->view('main_view');
    }

    public function create ()
    {
        $this->load->view('create_view');

    }
}

If I want to create a relative link to create, how do I accomplish that? The following link in view doesn't always work. What is apporpiate way to create relative links in CodeIngiter?

<a href="create"> Create </a>
Mark
  • 41
  • 1
  • 1
  • 2

3 Answers3

4
<a href="<?= site_url('/main/create'); ?>"> Create </a>

or simply:

<?= anchor('/main/create', 'Create'); ?>

Make sure you have loaded the URL Helper.

jáquer
  • 304
  • 1
  • 9
3

You don't have to do anything special or load any helpers, just keep in mind that paths will be relative to the url and not the filesystem or controller.

Assuming your installation is in the root directory of your domain, let's say your current URL is http://localhost/class/method/var:

<a href="/main/create">Will work from anywhere</a>
<a href="create">Will go to http://localhost/class/method/var/create</a>
<a href="../create">Will go to http://localhost/class/method/create</a>

Relative paths are not your friend in Codeigniter, you are better off sticking with full urls (typically using the helper functions like base_url() and site_url()), or to use the forward slash (relative from root). People have mentioned using the <base> html tag, but I don't personally recommend it. You are going to have some very wacky urls if you use ../../relative paths when you get deeper into the url segments. Example:

If you are here: http://localhost/controller/method/var1/var2/var3

A link might look like this: <a href="../../../../controller2/method/othervar"></a>

Probably not what you want, but it's an option you may choose. I recommend using one of the other two.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
0

Just to point another alternative, if you dont like the idea of writing a php fragment in each href, and if the other approaches don't satisfy you. You can use put a common <BASE > tag in your html header (for example that points to the root of your application), and then remember that every relative url in your pages will be with respect with that url.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • `` tag can cause problems, see here: [http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag](http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag) – Wesley Murch Apr 18 '11 at 00:01
  • @Madmartigan Arguable. I'd seen that post before I decided going for this `` alternative. And I had no problems. One must be aware of the issues mentioned in the accepted answer, but that's all. And see also the top voted answer. – leonbloy Apr 18 '11 at 00:29
  • Yes I read it, the top voted answer concludes that it should be avoided. I know from experience that it can break in-page hash anchors and third party scripts. I have used it and am using it on some sites now that "need" it, but it should be used with caution. – Wesley Murch Apr 18 '11 at 05:34