__remap() Function in CodeIgniter 4 | Method Remapping
In CodeIgniter 4, we can remap function calls with the use of the function in controller called _remap() method. The __remap() method will call every time that the controller is called even if a different function is used. Simply, we can say that remap() function will remap URI requests to specific controller methods.
It’s double underscore before remap().
CodeIgniter will call the _remap() function before calling any other function. It’s pre defined method. Inside this article we will see the concept of __remap() function in CodeIgniter 4.
Here is the command to install via composer –
$ composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
CodeIgniter __remap() Method
If controller contains a method named _remap(), it will always get called regardless of what URI contains. It overrides the normal behaviour in which the URI determines which function is called, allowing to define own function routing rules.
Syntax
public function _remap() { // Some code here... }
__remap() Method — Example
Here, we will understand about the concept of __remap() method with the help of an example.
Let’s say we have an application and a url from application as
URL
http://localhost:8080/index.php/site/indexORhttp://localhost:8080/site/index
If we break URL into controller and method then we can write as –
- Controller — Site
- Method — index
When we hit given URL it will call index method of User Controller.