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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With