Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Redirect to current URL (with URL params)

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: 2065 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Redirect to current URL (with URL params) Link to this post

    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.

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Redirect to current URL (with URL params) Link to this post

    Did you mean something like that:

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

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: Redirect to current URL (with URL params) Link to this post

    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");

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Redirect to current URL (with URL params) Link to this post

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

  • MightyQuinn
    Avatar
    Community Member
    1 Post

    Re: Redirect to current URL (with URL params) Link to this post

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

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