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.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

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

URL "Action" is seen as a path even when Nested URLs is disabled.


Reply


4 Posts   1049 Views

Avatar
micahsheets

Community Member, 165 Posts

26 May 2011 at 5:58am

I have a site that I upgraded from 2.3.7 to 2.4.5. On the site I use the URLParams['Action'] in my controller. However since the upgrade I get a page not found error because 2.4.5 doesn't seem to handle the URLParams the same as 2.3.7.

I am not actually using the Action as an action on the controller just as a way to pass variable data in to the index action.

My url looks like this:

http://example.com/locations/3

In my script I have this code which is called from the template in a public function. But the function never gets called because the page is redirected to page not found because the site thinks that I am asking for a page with the urlSegment of 3 that is a child of Locations. At least that is what I think. I have //SiteTree::enable_nested_urls(); in my config so it should not see the third section of the url as a link but instead as the action.

      if ($this->URLParams['Action']){
         $clinic = DataObject::get_by_id('Clinic', $this->URLParams['Action']);
         if ($clinic){
            return $clinic;
         }
      }
      else {
         $clinic = DataObject::get_one('Clinic', "Name = 'Spokane Clinic'");
         if ($clinic){
            return $clinic;
         }
      }
      return false;

Does 2.4.5 have a different way of parsing the url?

Avatar
LisaB

Community Member, 28 Posts

26 May 2011 at 7:20am

I'm sure someone with more knowledge than me can help out further as I am new to all this and I may be on the wrong track here but I had a similar problem earlier today which seemed to be caused by the fact that the action I was trying to use was not listed as an allowed action on the controller. I'm not sure if this is something that has changed in 2.4.5, but what I was trying to do was similar and I ended up with URL as pageurl/edit/3 for example, with the following in my controller:

static $allowed_actions = array(
'edit'
);

function edit() {
//calls function get habit below and does some stuff with the retrieved object
}

public function getHabit() {
$Params = $this->getURLParams();
if(is_numeric($Params['ID']) && $Habit = DataObject::get_by_id('Habit', (int)$Params['ID'])) {
return $Habit;
}
}

SSBits has a tutorial that does something similar: http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-1-keeping-it-simple/ - thanks to SphereSQL on the IRC for telling me about this and helping me out with this problem earlier on. The way I'm doing this may be overkill, but it does work :-)

Avatar
MarcusDalgren

Community Member, 288 Posts

27 May 2011 at 12:14am

Do you have a page with the URL locations or is that missing as well?

If you have a page called locations you should be able to get it to fall back to the index action if no other action is defined. There you can get the action with $this->getRequest()->latestParams(). Another thing to note though is that SilverStripe will still send a status header of 404 so you'll have to set that manually yourself. To set the response you do $this->getResponse()->setStatusCode(200).

Avatar
micahsheets

Community Member, 165 Posts

4 June 2011 at 6:56am

The answer to this question seems to be that in 2.4.5 it is not possible to simply send an "Action" in the url and pars it with $this->URLParams['Action']; like you used to be able to do. Now there must be an action function that at the very least sends out the page with a renderWith function call. Since I often just want to use the "Action" in the URLParams to send info to the controller and not actually run an action this is a little frustrating. So now I just make a dummy "Action" and use ID or Other ID to send my data.