I have a PHP CodeIgniter Controller with name User
and have a method that get details of user user_detail($username)
Now when i need to show user data for example for userName mike
I call this URL
http://www.example.com/user/user_detail/mike
My target
How to make user data accessible by next URLs
http://www.example.com/user/mike
or / and
http://www.example.com/mike
Hatem's answer (using the route config) is easier and cleaner, but pointing the usage of the
_remap()
function maybe helpful in some cases:inside the
CI_Controller
, the_remap()
function will be executed on each call to the controller to decide which method to use. and there you can check if the method exist, or use some defined method. in your case:application/controllers/User.php
this will result:
but it's not going to work with: http://www.example.com/mike , since the controller -even if it's the default controller- is not called at all, in this case, CI default behaviour is to look for a controller called
mike
and if it's not found it will throws 404 error.for more:
Codeigniter userguide 3: Controllers: Remapping Method Calls
Redirect to default method if CodeIgniter method doesn't exists.
You have to read the this page from the official documentation of codeigniter. It covers all related things to Routing URLs easily. All routes must be configured via the file:
It could be something like this :
This can be achieved by overriding
CI_Controller
class BUT dont change the original core files, like I said override the controller and put your logic in it.Help: https://ellislab.com/codeigniter/user-guide/general/core_classes.html
how to create Codeigniter route that doesn't override the other controller routes?
Perhaps an easier solution would be to route it with the help of apache mod_rewrite in .htaccess
Here is an detailed explanation on how to achieve it: http://www.web-and-development.com/codeigniter-remove-index-php-minimize-url/