Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any way to remove or hide the controller name in 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 looks like this:

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

and my manager wants it to look like this:

http://www.sitename.com/function_name

Note that I have more than 10 controllers in my project and many many functions. I want a method that applies to all. HELP PLEASE.

like image 613
Ajmal Razeel Avatar asked Sep 21 '16 11:09

Ajmal Razeel


2 Answers

You can do this using $route.

If you have only one Controller and in that controller you all the functions you can write something like this:

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

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

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

And you can't have duplicates in $route

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

You can check more about routes on the CI documentation here: https://www.codeigniter.com/userguide3/general/routing.html

like image 198
Daniel Dudas Avatar answered Nov 14 '22 22:11

Daniel Dudas


You have to specify your controller and method name in routes file like below

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

You have to write this for each function in controller.By doing you get two advantages

1) Controller name will be hidden in your url 2) you can call some method by its name only.No need to use controller name every time.

like image 28
Shaunak Avatar answered Nov 14 '22 23:11

Shaunak