Route CodeIgniter canceled by controller / method
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.
Route CodeIgniter canceled by controller / method
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:
- MOST DEFINED
- LESS DEFINED
- 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
-
105
Nowadays lots of PHP frameworks like CodeIgniter, Laravel and Yii are being used for websites development. If you are using PHP for your web application....
-
109
-
48
Bob LennesFollowSeasoned PHP CodeIgniter, jQuery, and SQL ExpertImplementing a "Dynamite" Search...
-
80
-
66
PayPal provides various types of API to integrate payment gateway in the web application. Mostly, the Standard Payment Gateway is used to accept payment online with PayPal.
-
48
CI3 ready admin dashboard Ready admin panel with ACL(user access control),Users , Category , Language and few other common features. VIEW TH...
-
237
README.md ujian-online-ci Aplikasi Ujian Online Menggunakan CodeIgniter Catatan Disarankan upgrade PHP ke versi terbaru (7.3...
-
9
Codeigniter Load the parameter at the index in the default controller according to the URL parameter advertisements I want to load a variable...
-
4
The angular Js connecting a controller to a route do not work advertisements I watching an Angular course on codeschool and it say that there are t...
-
7
Codeigniter - print $ Controller- & gt; Session without knowing the key? advertisements Instead of doing th...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK