Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Different routes to same controller


Reply

7 Posts   490 Views

Avatar
Andre

27 September 2013 at 10:11pm Community Member, 126 Posts

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:
<code>
---
Name: customroutes
After: framework/routes#coreroutes
---
Director:
rules:
'' : 'AppController'
'app//$Action/$ID/$OtherID' : 'AppController'
'api/mainsymptoms//$Short' : 'ApiController'
'api//$Action/$ID/$OtherID' : 'ApiController'
</code>
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?

Avatar
martimiz

28 September 2013 at 1:22am Forum Moderator, 1091 Posts

Have you tried the other way around? That is: the general one first, the specific one last? Later might overrule earlier...

Avatar
Andre

28 September 2013 at 3:02am Community Member, 126 Posts

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:

/api/mainsymptoms/$ID/relatedymptoms/$RelatedID

Avatar
martimiz

28 September 2013 at 4:46am Forum Moderator, 1091 Posts

Out of interest I 'hacked' a default 3.1 site with the following settings (had no custom controller ready):

---
Name: customroutes
After: framework/routes#coreroutes
---
Director:
rules:
'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:

mydomain/test/something/else:

Short => else

mydomain/test/nothing/else:

Action => nothing
ID => else
OtherID =>

(and a bunch of errors, but that's to be expected :) ) So that seems to be working as it should...

Avatar
Andre

1 October 2013 at 7:16pm (Last edited: 1 October 2013 7:16pm), Community Member, 126 Posts

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:

array (size=1)
'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?

Avatar
martimiz

1 October 2013 at 9:38pm Forum Moderator, 1091 Posts

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...

Avatar
Andre

2 October 2013 at 12:06am Community Member, 126 Posts

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.