Welcome Guest, Not a member yet? Register   Sign In
Invoking a subset within a CI application
#11

[eluser]Kenneth Allen[/eluser]
OK, that seems to make sense.

How does one implement the logic to handle a URL such as
Code:
http://my_site.com/schedule/season/1/program/2/Team/3
where the intent is to view the information associated with a specific team within a specific program or division within a specific season? Which controller or controllers need to be written receive the URL? It is possible to pass symbolic names rather than internal ID numbers, so the URL might look like the following?
Code:
http://my_site/schedule/season/Summer2009/program/Juniors/team/Bucaneers
Is it as simple as checking the parameter and if it is an integer presume that it is an ID and otherwise that it is a key value?

I do not see how CI would support a hierarchy of information such as that in the URLs shown, but it even if the user is forced to traverse links from page to page, the information has to be passed. So the shortest URL might display a list of seasons, and clicking on one of those would display information about that season, which would be in its own controller. But what happens when on clicks on a program within that season?

I tried adding the following line to my routes.php file, but when I then invoke the URL above, instead of getting the 404 page I just get a blank page!
Code:
$route['season/(:any)/program/(:any)/team/:any')] = 'team/info/$1_$2_$3'
I am new to CI and while I have figured a number of issues out, I remain unfamiliar with the definitions required for routes to work properly.
#12

[eluser]Colin Williams[/eluser]
Quote:How does one implement the logic to handle a URL such as

One reads the User Guide and learns that the URI class has magical functions like uri_to_assoc()

Quote:Which controller or controllers need to be written receive the URL?

Well, if you're being RESTful, you need to think about what your Resources are, and have a controller for each. To me, it looks like the resource is actually Programs. Season is just a parameter, like team or grade.

Quote:I tried adding the following line to my routes.php file, but when I then invoke the URL above, instead of getting the 404 page I just get a blank page!
Code:
$route['season/(:any)/program/(:any)/team/:any')] = 'team/info/$1_$2_$3'

Couple of things.

First of all, there is a glaring syntax error there.

Second, this route says that you have a Team controller with a function "info." Is this correct? You should probably have a Program controller, with the index function handling the, er, indexing. Or, Teams may very well be a resource. In this case, I would consider a more appropriate URI, like /team/14/season/2009/grade/10. A good rule of thumb to follow is /resource/action/guid/params. When you starting doing, /params/params/resource/guid, routing becomes more of a headache.

Third, all you need to settle for the URI to be routed is the controller and the function. Doing param/match/param/match/... is woefully rigid. A route as follows is more appropriate.

Code:
$route['season/(.*)'] = "program/index/season/$1";

This means /season/2009/team/buccaneers gets routed to /program/index/season/2009/team/buccaneers and /season/2009/team/buccaneers/grade/10/fname/steve gets routed to /program/index/season/2009/team/buccaneers/grade/10/fname/steve. And then, with uri_to_assoc(), you get an array like

Code:
array(
  'season' => '2009',
  'team' => 'buccaneers',
  'grade' => '10',
  'fname' => 'steve',
);

... which you can use when querying the records. Again, consider doing /program/index/season/2009 and kiss the need for routing goodbye. Hope I've covered it...

(And also, it's frustrating to see--still, at this day and age--so much misconception about REST.. POSTing hidden fields to GET resources!? Eh!? Multiple URIs for a single resource/result set!? Eh!?)
#13

[eluser]fijiaaron[/eluser]
If 'program' is 'juniors' then I see it as just another parameter to 'schedule'.

If I understand correctly, it seems like each case is actually an instance of an 'index' or 'view' method for 'schedule', though I'm a bit unclear about the expected behavior.

If what you want is to hide information in a session so the URL can be shorter, you could store parameters from previous requests in a cookie.

So my take:

A schedule is the model you're returning.
The parameters used to determine which schedule to return include:
season (e.g. Summer2009)
team (e.g. Buccaneers)
program (e.g. Juniors)
Are all parameters needed to identify a schedule?
Do you want to return a list of schedules that meet the criteria or options to select further parameters if a specific schedule isn't identified?

I agree with Colin that having 1 canonical URL for each resource is a good idea, but I also understand Kenneth wanting a more user-friendly interface. You could satisfy both by processing "shorthand" urls but forcing a redirect (though suffering a performance hit.)

REST is nice, but is only an opinion of how an API can be developed, not THE WAY THE WEB ACTUALLY WORKS. While you can't GET hidden form fields you could certainly POST information and also obtain parameters from the URL (though CodeIgniter doesn't really help you with that, except with a few helper functions like uri_to_assoc().)


Mapping routes for all the possible combinations could get tricky, and I wouldn't rely on matching like I did with 'season' in my example posted above. You could keep all parameters in order, or use uri_to_assoc() as Colin suggested, or implement your own URI parser (e.g, for named parameters http://my-site/schedule/season=summer200...am=juniors) or just use a good old fashion query string (e.g. http://my-site/schedule?program=juniors&...summer2009)




Theme © iAndrew 2016 - Forum software by © MyBB