Routing and controller arguments in K3
Shad Laws <shad-xpYdmXCiSuZWk0Htik3J/[email protected]> Thu, 11 Apr 2013 00:32:15 +0200
| Newsgroups | gmane.comp.web.gallery.devel |
|---|---|
| Message-ID | <CA+z51A6-ev495fZNm-R5GRyUv=20_ysG7HOVsLMCPs_8NLzfMQ@mail.gmail.com> |
Hey gang,
So, calling controller actions with arguments was carried over to 3.0,
deprecated in 3.1, and removed in 3.2.
Example: we want index.php/bar/foo/123. What used to be this:
public function foo($id=null) {
// Do something with $id = 123...
}
Will now need to be something like this:
public function action_foo() {
$id = $this->request->param("id", null);
// Do something with $id = 123...
}
With a route defined like this:
Route::set("example", "(<controller>(/<action>(/<id>)))")
A quick survey of Gallery's controllers (in the core repo) shows that,
in 162 controller actions, we have:
- 81 with 0 args
- 70 with 1 arg (typically named something like $id)
- 9 with 2 args
- 1 with 3 args
- 1 with 4 args
- 0 with 5+ args
So, here's my proposal: we optimize ourselves for 0-1 arguments, and
make a flexible system to handle 2+. We use a route something like
this:
Route::set("example", "(<controller>(/<action>(/<id>(/<args>))))")
->filter( // Use an explode for "/" to parse args into an array here )
And then access them like this:
$id = $this->request->param("id");
$args = $this->request->param("args");
$second = $args[0];
$third = $args[1];
$fourth = $args[2];
$fifth = $args[3];
...
$fiftieth = $arg[48];
This keeps our typical cases simple and lean, while being totally
flexible for our other 11 cases as well as anything a contrib module
could dream up. Does this seem like a reasonable approach?
Take care,
Shad
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
__[ g a l l e r y - d e v e l ]_________________________
[ list info/archive --> http://gallery.sf.net/lists.php ]
[ gallery info/FAQ/download --> http://gallery.sf.net ]