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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Different routes to same controller


Go to End


7 Posts   1332 Views

Avatar
Andre

Community Member, 146 Posts

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

Forum Moderator, 1391 Posts

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

Avatar
Andre

Community Member, 146 Posts

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:

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

Avatar
martimiz

Forum Moderator, 1391 Posts

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):

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

Community Member, 146 Posts

1 October 2013 at 7:16pm

Edited: 01/10/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:

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

Forum Moderator, 1391 Posts

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

Avatar
Andre

Community Member, 146 Posts

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.