How to create a Codeigniter route that does not override other controller routes?

I have many controllers in my Codeigniter applications, for example: Registration, Profile, Main, etc.

Now I want to create a User controller.

what I want:

  • if people access the URL: example.com/signup , I want to use the default route for " Register "
  • if people access the URL: example.com/bobby.ariffin , I want to redirect this to " User " because the URL is not handled by any controller in my applications.

I created this in my config / routes.php:

$route['(:any)'] = "user"; 

but it redefines the entire route in my applications using " User ".

Is there a simple route for Codeigniter that does not override other controller routes?

Update ---

I have a simple regex for this problem: Daniel Erante Blog

 $route['^(?!ezstore|ezsell|login).*'] = "home/$0β€³; 

where ezstore, ezsell and login is the name of the controller in your applications.

+4
source share
2 answers

You will need to explicitly define all of these routes. Otherwise, you will always find yourself in "user_controller".

 $route['signup'] = "signup"; $route['(:any)'] = "user/display/$1"; 

or something similar. They run in order, so what is ever determined first will happen first. Therefore, if you catch (: any), you are going to send NOTHING to this controller.

Also keep in mind that you can use regular expressions, so if you know that there will always be a ".". there you can check it out.

+3
source

You can also use the foreach statement for this. That way, you can keep your controllers on a good neat list.

 $controller_list = array('auth','dashboard','login','50_other_controllers'); foreach($controller_list as $controller_name) { $route[$controller_item] = $controller_name; } $route['(:any)'] = "user/display/$1"; 
+5
source

Source: https://habr.com/ru/post/1305353/


All Articles