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

Redirect to current URL (with URL params)


Reply


5 Posts   2749 Views

Avatar
Double-A-Ron

Community Member, 604 Posts

16 July 2013 at 9:35pm

Edited: 16/07/2013 9:40pm

This one has been hard to track down. So I know I'm missing something really simple.

Basically, I have a page with a form. This form submits to the current page, performs an action, and then reloads the page. This code is in my base Page.php. Which is extended by multiple other page types.

Some of those pages have url parameters. Like so:

/page?tid=34

All I want is to be able to redirect to the current page by using:

Director::redirect(Director::get_current_page()->Link());

This works fine when there is no url params, but obviously the params a missing after the redirect on pages that have them.

Does Silverstripe have a method of getting all url params as per PHP's simple $_GET array or QUERY_STRING?

Note: I have seen Director::urlParam('tid') will work above. Basically I want this method to be flexible enough to simply get them all and rebuild the URL as it was for redirect.

Avatar
Bereusei

Community Member, 94 Posts

17 July 2013 at 1:00am

Did you mean something like that:

Director::redirect($this->Link("?success=1"));

Avatar
Futureweb

Community Member, 233 Posts

17 July 2013 at 4:18pm

In SS3 you need to use 'Controller' instead of 'Director'

I also has a few issues doing a very similar thing, and I ended up with the following instead:

$referrer = $_SERVER['HTTP_REFERER'];
Controller::redirect("$referrer?saved=1");

Avatar
Double-A-Ron

Community Member, 604 Posts

17 July 2013 at 4:36pm

Thanks guys. I was really checking to see if Silverstipe had a built in method of simply obtaining the pages current URL including the Query String. It looks like it doesn't.

Your examples will work, as long as "saved" or "success" are passed in the url. But in this case, the query string could be a number of things. Or not there at all for some pages. All I was looking for is get whatever is in the query string without resorting to plain PHP to keep things neat. I would have thought this would be common enough for it to warrant an existing helper.

I have solved this by making a method that simply returns the current URL in it's entirety. Placed this in my central Applicatoin file, and can now get any current URL in full by calling Application::curPageURL();

/**
    * Just get the current URL
    * @return string
    */
   static function curPageURL() {
      $pageURL = 'http';
      if (Director::protocol() == 'https') {$pageURL .= "s";}
      $pageURL .= "://";
      if ($_SERVER["SERVER_PORT"] != "80") {
         $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
      } else {
         $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
      }
      return $pageURL;
   }

Avatar
MightyQuinn

Community Member, 1 Post

4 April 2014 at 12:06pm

This was bugging me a lot too. I wanted to keep the url intact for a combination of search form for filtering data and pagination to be used with infinite scroll. I found some helpful methods in the SS_HTTPRequest class: http://api.silverstripe.org/3.1/class-SS_HTTPRequest.html

I overrode the Link() Method in my Controller like this:

public function Link($action='') {
      
      // Manipulate the URL So we can maintain GET Params from the Search Form
      $req = Controller::curr()->getRequest(); // get the current http request object
      $req->setURL(parent::Link($action)); // set the url of it to our new Link (while ignoring query params)
      $url = $req->getURL(TRUE); // get the url back but with querystr intact.
      
      return $url ;
   
}