__remap() Function in CodeIgniter 4 | Method Remapping

Reading Time: 4 minutes
6,739 Views

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.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


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... 

}

Example

//...

public function _remap($method)
{
    if ($method === 'some_method')
    {
        return $this->$method();
    }
    else
    {
        return $this->default_method();
    }
}

//...

__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/index

OR

http://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.

By using __remap() method of controller, we can map different method instead of index method.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class Site extends BaseController
{
	public function _remap($method)
	{
		if ($method == 'index') {

			$this->other_method();
		} else {

			$this->default_method();
		}
	}

	public function other_method(){

		//.. 

	}
}

When we hit URL – http://localhost:8080/site/index Instead of calling index method, Using __remap() method we remapped method from index() to other_method().


Passing Parameters To Remapped Method

When we hit URL – http://localhost:8080/site/index/param1/param2.

Here, we are passing param1, param2 into URL. So by this URL we can say these –

  • Controller – Site
  • Method – Index
  • Parameters – param1, param2

Using Remap Method

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class Site extends BaseController
{
	public function _remap($method, $param1, $param2)
	{
		if ($method == 'index') {

			$this->other_method($param1, $param2);
		} else {

			$this->default_method();
		}
	}

	public function other_method($param1, $param2){

		//.. 

	}
}

We hope this article helped you to learn about __remap() Function in CodeIgniter 4 | Method Remapping in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.