CodeIgniter - Correct way to link to another page in a view

164,090

Solution 1

I assume you are meaning "internally" within your application.

you can create your own <a> tag and insert a url in the href like this

<a href="<?php echo site_url('controller/function/uri') ?>">Link</a>

OR you can use the URL helper this way to generate an <a> tag

anchor(uri segments, text, attributes)

So... to use it...

<?php echo anchor('controller/function/uri', 'Link', 'class="link-class"') ?>

and that will generate

<a href="http://domain.com/index.php/controller/function/uri" class="link-class">Link</a>

For the additional commented question

I would use my first example

so...

<a href="<?php echo site_url('controller/function') ?>"><img src="<?php echo base_url() ?>img/path/file.jpg" /></a>

for images (and other assets) I wouldn't put the file path within the php, I would just echo the base_url() and then add the path normally.

Solution 2

The best way is to use the following code:

<a href="<?php echo base_url() ?>directory_name/filename.php">Link</a>

Solution 3

you can also use PHP short tag to make it shorter. here's an example

<a href="<?= site_url('controller/function'); ?>Contacts</a>

or use the built in anchor function of CI.

Share:
164,090
Cecil
Author by

Cecil

Updated on July 13, 2022

Comments

  • Cecil
    Cecil almost 2 years

    I was wondering if someone could tell me the correct way to link to another page from within a view.

    Is there a function for this or is it just the usual about

    Cheers,