23370 Posts in 18160 Topics by 2864 members
|Go to End|
27 September 2013 at 10:11pm
Hi there I'm trying to set up different routes with different parameters to different actions of the same controler.
The reason I#m trying this, is to achive a restfull api.
Here is my routes.yml, that doesn't seem to work:
'' : 'AppController'
'app//$Action/$ID/$OtherID' : 'AppController'
'api/mainsymptoms//$Short' : 'ApiController'
'api//$Action/$ID/$OtherID' : 'ApiController'
Normaly with the two api routes, I expect, to have one route for the mainsymptoms Actions with param('Short') and the standard fallsbacl to all other actions, but that doesn't seem to work.
So the question is, if and how I can achieve, to have different Parameters for different Actions of the same controller?
28 September 2013 at 1:22am
Have you tried the other way around? That is: the general one first, the specific one last? Later might overrule earlier...
28 September 2013 at 3:02am
Yes I tried it, but then, the first one also matches for action mainsymptoms. So that instead of having $Short set in mainsymptoms action, I need to read from $ID.
Is it also possible, to define routes, that include encapsulated relations?
Something like the following:
28 September 2013 at 4:46am
Out of interest I 'hacked' a default 3.1 site with the following settings (had no custom controller ready):
'test/something//$Short' : 'Page_Controller'
'test//$Action/$ID/$OtherID' : 'Page_Controller'
and added this to the Page_Controller::init() function:
foreach ($this->urlParams as $k => $v) echo "$k => $v <br>";
this is what I got:
Short => else
Action => nothing
ID => else
(and a bunch of errors, but that's to be expected ) So that seems to be working as it should...
1 October 2013 at 7:16pm Last edited: 1 October 2013 7:16pm
The Problem is, that the controller doesn't seem to route to the correct Action.
Dumping $this->urlParams within the init method of my API Controller, I get the correct result:
'Short' => string 'h' (length=1)
But as there seems no Action to be set, it always responses with a "Page not found" instead of opening the correct action.
So, how do I tell the Route, that on "test/something//$Short" something is the Action?
1 October 2013 at 9:38pm
Ok... I guess if 'mainsymptoms' is not an actual page, the example won't work because it is fixed and there is no matching Action parameter in the rule.
Obviously I could be all wrong, but I'm guessing your reason for this construction is so that in the mainsymptoms() funtion you can use a param called 'Short' instead of 'ID', as it's nor really an ID? Technically there is nothing against using $ID and $OtherID...
2 October 2013 at 12:06am
You are right, technically there is nothing against $ID and $OtherID, it just seems a bit ugly and is not that nice. My Idea was more to create a restful api like I'm able to in RoR, but it seems, that currently Silverstripe doesn't support that. So I have to go with $ID and $OtherID (or whatever I will call them) throughout the whole controller.
|Go to Top|