Is there any way to remove or hide the controller name in the url? CodeIgniter

I developed a project in php codeigniter. The project is almost complete, but now my manager wants me to completely remove or hide the controller names in the URL. My current url is as follows:

http://www.sitename.com/Controller_Name/function_name

and my manager wants it to look like this:

http://www.sitename.com/function_name

Please note that I have more than 10 controllers and many functions in my project. I need a method that applies to everyone. HELP PLEASE.

+5
source share
2 answers

You can do this using $route .

If you have only one Controller , and on this controller you all functions can write something like this:

 $route['(:any)'] = "Controller_Name/$1"; 

If you have many Controllers , you need to specify for each function what the controller indicates. Like this:

 $route['function1'] = "Controller_Name1/function1"; $route['function2'] = "Controller_Name1/function2"; $route['function3'] = "Controller_Name3/function3"; 

And you cannot have duplicates in $route

This array $route must be located here: application/config/routes.php .

Here you can find out more about the routes in the CI documentation: https://www.codeigniter.com/userguide3/general/routing.html

+7
source

You must specify your controller and method name in the routes file, as shown below

$ route ['function_name'] = 'control_name / function_name';

You must write this for each controller function. You get two benefits

1) The name of the controller will be hidden in your URL 2) you can call some method only by its name. You do not need to use the controller name every time.

+1
source

All Articles