In this article, we will explain to you how to get the current URL in Codeigniter. we can easily retrieve the current Url using the Codeigniter helper URL. so first we have to load the helper URL after then we can fetch the URL.
Codeigniter is the most powerful framework of PHP. it provides a URL Helper library. we need to just load that library after loading the library we can easily get the Url. Codeigniter helper Url provide many types of function such as site_url(), base_url(), current_url(), uri_string(), index_page(), anchor() and etc. you can also read or visit on official guideline. so you can see below the following example.
Get current URL in codeigniter
sometimes we need to get the current URL in the controller. Codeigniter provides the inbuilt library. so we can easily get the current URL using the current_url() method.
$this->load->helper('url'); $page_url=current_url(); echo $page_url;
how to get URL segment in codeigniter
sometimes we need to get the Segments URL in the controller. Codeigniter provides the inbuilt library. so we can easily get the Segments URL using the segment() method.
$this->load->helper('url'); echo $this->uri->segment(1); echo $this->uri->segment(2);
Get current controller name in codeigniter
sometimes we need to get the current controller name in the Codeigniter. Codeigniter provides the inbuilt library. so we can easily get the current controller name using the fetch_class() method.
$this->load->helper('url'); echo $this->router->fetch_class(); //It will returns controller name
Get Current Controller Method Name in codeigniter
sometimes we need to get the current controller method’s name in the Codeigniter. Codeigniter provides the inbuilt library. so we can easily get the current controller method’s name using the fetch_method() method.
$this->load->helper('url'); echo $this->router->fetch_method(); //It will return current method name