Welcome Guest, Not a member yet? Register   Sign In
Feature Request: Routing
#1

This is a feature request for whichever is the next iteration of CI, whether it be 3.1 or 4.0, and since there isn't a dedicated page for it I'll post it here.

Initially I was a big fan of the "magic" routing in Codeigniter as it made it easy to just create a controller and get going. But as time went on and hundreds of controllers have been created, I've had to do a lot of routing so specific urls go to the right location (example: "/profile/1/show");

The issue with this is that by doing it this way, you have 2 open endpoints to the same location in your app. So if I have

Controller: Comments
Method: show($id)
Route: "/comments/(:num)/show" = "comments/show/$1"

I can actually hit this same method by going to both /comments/1/show and comments/show/1.

As you can imagine, this might cause an issue down the road if you have endless API endpoints and so forth.

My Proposal:


I propose there be a simple toggle in your config file for the automatic routing. By having it default to "On", it will prevent any breaking changes to previous versions of codeigniter, and by turning it "Off" we give the developer the choice of having to write their own routes.
Reply
#2

I'm not sure why it matters though if you can get to a controller directly or via the route. I'd assume you're only creating links, or documenting your API, with the routed url and not the original, so you won't get duplicate content if google doesn't have 2 different links going to the same location to crawl. They'd just know about the routed link you created.

Your solution of "on" and "off" wouldn't work for a lot of sites. Like, most of my controllers aren't routed, but a few are. What if I have 50 controllers and only want to reroute 5 of them? I'd have to create routes for all 50.
Reply
#3

(This post was last modified: 02-12-2015, 04:04 PM by albertleao. Edit Reason: Typos )

(02-12-2015, 03:43 PM)CroNiX Wrote: I'm not sure why it matters though if you can get to a controller directly or via the route. I'd assume you're only creating links, or documenting your API, with the routed url and not the original, so you won't get duplicate content if google doesn't have 2 different links going to the same location to crawl. They'd just know about the routed link you created.

Your solution of "on" and "off" wouldn't work for a lot of sites. Like, most of my controllers aren't routed, but a few are. What if I have 50 controllers and only want to reroute 5 of them? I'd have to create routes for all 50.

I see where you're coming from and understand. 

One of the reasons I'm suggesting this is for people, like myself, who have a background with other MVC frameworks which require routes for all endpoints. One of the reasons I'm a fan of routing is for documentation purposes. Even though I use CI, I map all my routes in the routes file so that developers behind me can easily get a map of my entire app without having to open all the controllers and look at their methods.

Again, if you built the application the way you described, you'd simply leave the routing as "On" and go about your business. This suggestion doesn't really limit anyone but adds an extra configuration to those who might want it.

I personally like to have full control of my routes and feel that my URL's shouldn't be tied to what the name of my controller is. I also like being able to kill a route if I don't want someone accessing it.
Reply
#4

Phil Sturgeon wrote a good article about routing here which explains a lot of the benefits: 

https://philsturgeon.uk/blog/2013/07/bew...e-to-evil/
Reply
#5

(This post was last modified: 02-12-2015, 04:35 PM by CroNiX.)

I know it's not what you want, but as a temp workaround you can always use the uri helper and compare the routed uri in the controller, for controllers that have routes, and throw a 404 if it isn't coming in via a route. That would fix the duplicate endpoint issue and only allow the routed uri to work.
Reply
#6

(02-12-2015, 04:35 PM)CroNiX Wrote: I know it's not what you want, but as a temp workaround you can always use the uri helper and compare the routed uri in the controller, for controllers that have routes, and throw a 404 if it isn't coming in via a route. That would fix the duplicate endpoint issue and only allow the routed uri to work.

That could work.

This isn't a "end of the world" issue for me, but I figured it would be helpful and a decent feature request for the next iteration of CI.
Reply
#7

Check out the AltoRouter at GitHub.  There is no need to make a new feature of it.  I basically took the AltoRouter and whittled it's essential features into my Page Class at BootPress.  There we only have one Controller to rule them all, and when you create a "folder" the $page->routes() will map it from there, AND assign the params of a successful match to whatever variables you like.  Just copy the routes() and altoRouter() methods from the class and you could easily adapt them to your needs.
Reply
#8

(02-12-2015, 08:03 PM)paralogizing Wrote: Check out the AltoRouter at GitHub.  There is no need to make a new feature of it.  I basically took the AltoRouter and whittled it's essential features into my Page Class at BootPress.  There we only have one Controller to rule them all, and when you create a "folder" the $page->routes() will map it from there, AND assign the params of a successful match to whatever variables you like.  Just copy the routes() and altoRouter() methods from the class and you could easily adapt them to your needs.

Interesting!
Reply
#9

(02-12-2015, 02:10 PM)albertleao Wrote: I propose there be a simple toggle in your config file for the automatic routing. By having it default to "On", it will prevent any breaking changes to previous versions of codeigniter, and by turning it "Off" we give the developer the choice of having to write their own routes.

This is a semi-officially (as in: I've got plans for it, but haven't shared them with anyone yet) planned feature.
Reply
#10

(02-13-2015, 03:58 AM)Narf Wrote:
(02-12-2015, 02:10 PM)albertleao Wrote: I propose there be a simple toggle in your config file for the automatic routing. By having it default to "On", it will prevent any breaking changes to previous versions of codeigniter, and by turning it "Off" we give the developer the choice of having to write their own routes.

This is a semi-officially (as in: I've got plans for it, but haven't shared them with anyone yet) planned feature.

Well would you look at that! Sounds great. This seems like a simple enough change to be implemented in a future version of codeigniter. Thank you for the feedback Narf. 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB