how to get current url in codeigniter with Examples

Today, We want to share with you how to get current url in codeigniter.In this post we will show you base url in codeigniter, hear for url segment codeigniter 3 we will give you demo and example for implement.In this post, we will learn about codeigniter gets Live url with an example.

How to get current url in controller or view ?

Codeigniter is the most powerful framework of PHP. it produce a URL Helper library. I need to just load that library after loading the library we can simply gets the Url. CI helper Url provide many types of method such as site_url(), base_url(), current_url(), uri_string(), index_page(), anchor() and etc.

PHP - How to Get current url using CodeIgniter controller's URL from a view
PHP – How to Get current url using CodeIgniter controller’s URL from a view

I can simply retrieve the current Url using the Codeigniter helper URL. therefore first I have to load the helper URL after then I can fetch the URL.

in CI current_url() through I can retrieve live url in your PHP CI Web application. But, you should load “url” helper before use current_url(). you can load url helper as bellow:

$this->load->helper('url');

In CI the term, Current URL refers to the complete URL of the live webpage being loaded and Base URL is the domain name or path to the application root (on localhost).

PHP Code igniter comes with URL Helper to assist you in working with application URLs. This helper contains bunch of methods to access URLs and in order to use them, you must first load the URL helper either inside controller or auto load it using autoload.php file.

To load URL Helper use this statement,

Or add this below line to ‘config/autoload.php’

$autoload['helper'] = array('url'); 

You have to use current_url() method to retrieve the live url in codeigniter. This method can be used anywhere inside codeignitor controller, model or view file and returns the full url of the webpage being viewed including the URI Segments (like controller, method name etc).

In bellow example

$this->load->helper('url');


$your_currentURL = current_url();
dd($your_currentURL);

Gets Current Controller Method Name in codeigniter

usually I need to retrieve the live controller method’s name in the Codeigniter. CI produce the inbuilt library. therefore I can simply retrieve the live controller method’s name using the fetch_method() method.

$this->load->helper('url');
echo $this->router->fetch_method(); 

//It will return live method name

Gets live controller name in codeigniter

usually I need to retrieve the live controller name in the Codeigniter. CI produce the inbuilt library. therefore I can simply retrieve the live controller name using the fetch_class() method.

$this->load->helper('url');
echo $this->router->fetch_class(); //It will returns controller name

how to retrieve URL segment in codeigniter

usually I need to retrieve the Segments URL in the controller. CI produce the inbuilt library. therefore I can simply retrieve the Segments URL using the segment() method.

$this->load->helper('url');
echo $this->uri->segment(1);
echo $this->uri->segment(2);

Gets live URL in codeigniter

usually I need to retrieve the live URL in the controller. CI produce the inbuilt library. therefore I can simply retrieve the live URL using the current_url() method.

$this->load->helper('url');
$page_url=current_url();
echo $page_url;

Codeigniter retrieve live url : Url helper is used to retrieve the live url in CI. It contains the method that returns the live url. The method current_url() is used to retrieve the live url in CI. Using this method you can retrieve live url anywhere on view, model, controller, Library or helper as well.

Url Helper contains the methods which assist in url working. You can load this helper in _autoload.php because this method is frequently used in codeigniter.

I hope you gets an idea about current url in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment