Jump to:

23358 Posts in 17874 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Different routes to same controller

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

Page: 1
Go to End
Author Topic: 445 Views
  • Andre
    Avatar
    Community Member
    126 Posts

    Different routes to same controller Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Different routes to same controller Link to this post

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

  • Andre
    Avatar
    Community Member
    126 Posts

    Re: Different routes to same controller Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Different routes to same controller Link to this post

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

  • Andre
    Avatar
    Community Member
    126 Posts

    Re: Different routes to same controller Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Different routes to same controller Link to this post

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

  • Andre
    Avatar
    Community Member
    126 Posts

    Re: Different routes to same controller Link to this post

    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.

    445 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.