4

Route CodeIgniter canceled by controller / method

 3 years ago
source link: https://www.codesd.com/item/route-codeigniter-canceled-by-controller-method.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Route CodeIgniter canceled by controller / method

advertisements

I have in my routes.php:

$route['ctrller1/method1/video/(:num)'] = 'ctrller2/method2/$1';

I also have a controller that is named ctrller1 that has a method:

function method1 ($str = NULL) {
    // do something
}

The problem is I have to use controller2 coz I can't or shouldn't edit controller1. What I want is seemingly simple but, apparently, CI doesn't want to work with me.

When the url:
domain.com/ctrller1/method1/edit
is invoked, I want the method inside ctrller1 to be called, if domain.com/ctrller1/method1/videos/1
is invoked I want the method in ctrller2 called.

It all seems correct to me but it won't work. So, I must be missing something. I've tried adding this to the routing:

$route['ctrller1/method1/(edit)'] = 'ctrller1/method1/($1)';

But it's a no go. Anyone see anything wrong here?


At any time when you work with routes, just like permissions (firewall, etc;) order is important. Typically you want to organize your routes in this order:

  1. MOST DEFINED
  2. LESS DEFINED
  3. GENERAL / FALLBACK

To clarify, that means your order for routes should be like this:

$route['ctrller1/method1/videos/view/(:num)'] = 'ctrller2/method3/$1';
$route['ctrller1/method1/videos/(:num)'] = 'ctrller2/method2/$1';
$route['ctrller1/(:num)'] = 'ctrller2/method1/$1';

When the URL is called, the route table goes through and finds the FIRST closest match, ELSE it traverses to the next route.

In this case what you want is something like this:

domain.com/ctrller1/method1/videos/1
domain.com/ctrller1/method1/edit

Reasoning for that is, the video's route is more specific, and also is a SPECIAL CASE, as you route it to another controller behind the scenes.

Here is what your routes should look like then (not tested, but should be it):

$route['ctrller1/method1/videos/(:num)'] = 'ctrller2/method2/$1';
$route['ctrller1/method1/edit'] = 'ctrller1/method1';

As a side, note, I am curious why you format it ctrller1/method1/videos/ and not something like ctrller1/videos/view/12355 or ctrller1/videos/edit/12355, the method1 seems confusing. But again I don't have all the details here.

Hope that works for you, if not comment, and I will revisit your question if you clarify it a little more.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK