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

Redirect to current URL (with URL params)


Go to End


9 Posts   20295 Views

Avatar
Double-A-Ron

Community Member, 607 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, 96 Posts

17 July 2013 at 1:00am

Did you mean something like that:

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

Avatar
JonShutt

Community Member, 244 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, 607 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 ;
	
}

Avatar
MarijnKampf

Community Member, 176 Posts

24 January 2016 at 1:19am

Found this post which nearly helped me so as a note to my future self, in SS3.1+ use:

Controller::curr()->redirect(...)

if you're not in a descendant of controller.

Avatar
soulrolll

Community Member, 9 Posts

13 February 2016 at 5:17pm

Edited: 13/02/2016 5:19pm

Still no solution for this?

I'm finding it impossible to do something as simple as add a simple subscribe to newsletter form on a footer / sidebar on 'all pages' without it sending me into controller on the url structure on submit.

None of the redirect codes provided here work unless i'm missing something, is someone able to put a complete form example up for a form taking you back to same page when submitted?

Avatar
Zanderwar

Community Member, 1 Post

16 September 2016 at 12:36pm

I just use

$_GET['url']

Can't really say using

$_GET
is a bad thing here, it comes from SS .htaccess and I'm sure URL injection is of no more concern here than what it is navigating to a broken link.

Go to Top