Categories
CodeIgniter

How to create links in codeigniter?

This Post is About Creating Links in CodeIgniter, To Create Links in CodeIgniter you have to load CI URL Helper,

How to load CI’s URL Helper:
with very simple line of code, you can load CodeIgniter Helper , here is the example :

How to Crate Links:
You can use fallowing codeingiter’s methods to create links : base_url() and site_url(), anchor()

below example shows how to make links with anchor method:

anchor tag with additional attributes

Below examples show links with site_url() function

site_url() : Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function, and the url_suffix as set in your config file.

base_url() : Returns your site base URL, as specified in your config file,This function returns the same thing as site_url, without the index_page or url_suffix being appended.This method is suitable for creating Style Sheet links , Script links and image links.

anchor() : anchor() function create the links,[anchor(uri segments, text, attributes)] If you are building links that are internal to your application do not include the base URL (http://…). This will be added automatically from the information specified in your config file. Include only the URI segments you wish appended to the URL.

If you have any queries you can mail me.

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

9 replies on “How to create links in codeigniter?”

In MVC approach we generally use segment based for example : example.com/className/MethodName/parameter1/parameter2/parameter3.

Now you can access parameters using URI class segment methods :

$this->uri->segment(3) // parameter1

$this->uri->segment(4) // parameter2

$this->uri->segment(5) // parameter3

you can also use traditional query string approach ,which can be enabled in your application/config.php file :
$config[‘enable_query_strings’] = FALSE;

$config[‘controller_trigger’] = ‘c’;

$config[‘function_trigger’] = ‘m’;

now the url structure becomes example.com?c=className&m=methodName&userID=10&profile_id=30, now you can access this

echo $this->input->get(‘userID’); // output 10

echo $this->input->get(‘userID’); // output 30

you can also pass parameters like example.com/ClassName/methodName?userID=10&profileID=30 ,with out enabling query string approach and you can access parameters same as above using get() method.

Leave a Reply

Your email address will not be published. Required fields are marked *