__remap() Function in CodeIgniter 4 | Method Remapping

Share this Article
Reading Time: 4 minutes
12 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.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed. Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

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.


Settings Environment Variables

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

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

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.

Find More on CodeIgniter 4 here

Leave a Comment