Routing issues with custom route in Codeigniter - php

My controller structure (api is the folder inside controller)
controllers/api/Api_1_0.php
controllers/api/Api_2_0.php
In my routes.php
$route['api/(\d+)\.(\d+)'] = "api/Api_$1_$2";
$route['api/(\d+)\.(\d+)/(:any)'] = "api/Api_$1_$2/$3";
The routing strategy i need is, if /api/2.0 is specified it will point to controller file Api_2_0.php
ie. api/x.y points to file Api_x_y.php
Everything working fine with above routing but below is my issue:
https://www.example.com/api/2.0/photos/1234567890 // not working
https://www.example.com/api/2.0/photos // working
How to solve ?

As mentioned in a comment you should change the order.
But you should also change (:any) to (.*). (:any) would only match first segment of your url.
So correct way would be:
$route['api/(\d+)\.(\d+)/(.*)'] = "api/api_$1_$2/$3";
$route['api/(\d+)\.(\d+)'] = "api/api_$1_$2";

Related

Setting Routing rules for different classes in codeigniter

My application URL "localhost/crdlabs/PHP" goes to "localhost/crdlabs/home/display/PHP" which is done using route.php. The rule is as follows.
$route['(:any)'] = "home/display/$1";
Now that I have another controller class called displayarticles(). The current URL for this class is "localhost/crdlabs/displayarticles/article/learning-coding". I understand that I cannot use the routing above for the new controller. How to set a routing rule for the current one to make the URL look like "localhost/crdlabs/learning-coding".
Note : learning-coding part is dynamically set which means there are several different articles that should go to the same controller.
Any help/advise please.
The current routing rule.
$route['(:any)'] = "home/display/$1";
Will routes anything appearing after localhost/crdlabs/argument to localhost/crdlabs/home/display/argument.So the route localhost/crdlabs/learning-coding will be redirected to localhost/crdlabs/home/display/learning-coding.So you can not use like this.
But I suggest you to show your articles
localhost/crdlabs/displayarticles/article/learning-coding
To
localhost/crdlabs/articles/learning-coding
using the following routing rule.
$route['articles/(:any)'] = "displayarticles/article/$1";
Will be best.

CodeIgniter routes with the same name

I have two different controllers which I want to route to the same URL.
For example,
$route['dashboard/(:any)'] = 'admin/crud/$1';
$route['dashboard/(:any)'] = 'admin/dashboard/$1';
But this is causing 404 errors.
I guess there is some issue with the :any wildcard.
Is there an alternative to use?
CodeIgniter doesn't map controllers to URLs, it maps URLs to controllers. See URI Routing.
You are trying to map two same exact URLs to go to different places. This doesn't make sense.
Also, since $route is just an associative array, you are overwriting the value instead of adding an additional route.
$route['dashboard/(:any)'] = 'admin/crud/$1';
$route['dashboard/(:any)'] = 'admin/dashboard/$1'; //Immediately over writes the previous value
So, it looks like you just have a problem with the second route:
$route['dashboard/(:any)'] = 'admin/dashboard/$1';
Since, admin is the folder, double check that the value being passed in by the route actually is a method in your dashboard controller class.
Also, check out this question and accepted answer: routing controllers in sub folders - codeigniter I think it provides an example of what you are attempting to do.

Routing Issue - Codeigniter

I have pages that are generated from a database, based on the URI. Functons as it should, however I can't set-up my routes to eliminate the controller and function from the URL.
$route['studios/(:any)'] = 'studios/location/$1';
Right now, I have the route to show the controller name and the URI variable (whatever that may be). However, I want to eliminate the controller name as well and just display the URI variable that's called as the URL. Hard to explain - hopefully someone picks up my drift...
Current URL would be: domain.com/studios/studio1
But I want to just display: domain.com/studio1
I tried $route['/(:any)'] = 'studios/location/$1';, but that's messing up my entire site.
Help?
$route['studios(/:any)*'] = 'studios/location';
This route will force everything from studios on to studios/location. You can then access any of the parameters using URI segments:
$id = $this->uri->segment(2);
If your URL was somewhere.com/studios/location/2, $id would resolve to 2
However, since you want it to just be from the root on, you will have to put your override route at the bottom of the routes file so it is assessed last:
// all other routes here. Which must be specifically
// defined if you want a catch all like the one you mentioned
$route['(:any)'] = 'studios/location';
Alternatively, if you want a high maintenance site, you can specify a collection of routes like so:
$route['(studio1|studio2|studio3)'] = 'studios/location/$1';
how is it "messing up your site"?
In any case, you should not have the / before (:any)
Just:
$route['(:any)'] = 'studios/location/$1';
EDIT:
BEFORE the $route['(:any)'], you'll need to specify routes fro all your controllers; this is pretty normal, don't know if I'd call it "high maintenance", but you'll need to decide

Codeigniter URL rewrite to hide controler and method name

My current url is universitytwig.com/user/userprofile/cm.cm , where user is the controller , userprofile is the method and cm.cm is the parameter .How i change this url to universitytwig.com/cm.cm by route.php or by routing from database, Or by .htaccess
Try to add
$route['cm.cm'] = 'user/userprofile/cm.cm';
to your application/config/routes.php
Or
$route['(:any)'] = 'user/userprofile/$1';
if your parameter can be different.
you can use the routes.php but if you make a rule in something like cm.cm to user/userprofile/cm.cm
$route['(:any)']= 'user/userprofile/$1';
But this is not a perfect solution it will point all the controllers of every thing after base url to user/userprofile/ you need to specify some string the CI routes will identify that this needs to be point on this controller if cm.cm is dynamic parameters not hard coded for this your url structure should be
universitytwig.com/students/cm.cm
and in routes you can now make a rule
$route['students/(:any)']= 'user/userprofile/$1';
This will work for your scenario

CodeIgniter __remap() routing more than one controller

I am trying to create a URL shortener using CodeIgniter 2.
I have 2 controllers: main and api.
For redirecting a short link through the router, I am using this setting in config/routes:
$route['(.*)'] = "main/$1";
along with a method in the main controller which should work. However, the controllers won't start. Please help me to solve this problem.
You controller "any" isnt called because it falls into that regex, so it's routed to main.
In order to exclude "any" from this rule you need to create a special rule for that, keeping in mind that for CI rules are cascading , so they're executed in the order they're presented
Note: Routes will run in the order
they are defined. Higher routes will
always take precedence over lower
ones.
So, you would have:
// reserved routes must come before custom routes
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['any'] = //your rule here. maybe "any". ?
$route['(.*)'] = "main/$1"; // CI also provides you with `(:any)` rule, that mateches any character.
More on this here: Uri routing

Categories