Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create Codeigniter route that doesn't override the other controller routes?

I've got a lot controller in my Codeigniter apps, ex: Signup, Profile, Main, etc..

Now I want to build "User" controller.

what I want:

  • if people goes to url: example.com/signup, I want use default route to "Signup" Controller
  • if people goes to url: example.com/bobby.ariffin, I want to reroute this to "User" Controller because the url not handled by any Controller in my apps.

I had create this in my config/routes.php:

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

but it's override all the route in my apps to "User" Controller.

Is there any simple route for Codeigniter that doesn't override the other controller routes?

Update---

I've got simple regex for this problem, from: Daniel Errante's Blog

$route['^(?!ezstore|ezsell|login).*'] = “home/$0″;

where ezstore, ezsell, and login are the name of controller in Your Apps.

like image 640
bakazero Avatar asked Mar 28 '10 10:03

bakazero


People also ask

What is URI routing in CodeIgniter?

URI Routing associates a URI with a controller's method. CodeIgniter has two kinds of routing. One is Defined Route Routing, and the other is Auto Routing. With Defined Route Routing, you can define routes manually.

How can add route in CodeIgniter?

All you need to do is to add the verb as an array key to your route. Example: $route['products']['put'] = 'product/insert'; In the above example, a PUT request to URI “products” would call the Product::insert() controller method.

Why is there a need to configure the URL routes in CodeIgniter?

URLs in CodeIgniter are designed to be short and search engine friendly. It should make more sense to the visitors. A user should get an idea about the page content through its URL.

What is use of routes in CodeIgniter 4?

Routing rules are defined in the app/Config/Routes. In it you'll see that it creates an instance of the RouteCollection class ( $routes ) that permits you to specify your own routing criteria. Routes can be specified using placeholders or Regular Expressions.


1 Answers

You can also use a foreach statement for this. That way you can keep your controllers in a nice 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";
like image 100
Lawrence Avatar answered Nov 15 '22 04:11

Lawrence