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.

Customising the CMS /

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

Create dynamic content according to URL variable


Go to End


5 Posts   3780 Views

Avatar
gratefulsound

Community Member, 2 Posts

19 February 2011 at 5:04am

Hi all,

I was just wondering what is the best way to assign a certain value to a variable according to a specific parameter in the URL.

For example, let's say I'm tagging backlinks to be implemented on an external website in this way www.mysite.com/page?source=externalsitename. I'd like to give the value "externalsitename" to a variable $Source so that i can call it from within my page templates to create dynamic content. For example, by adapting the landing page subheader to state "You were redirected from $ReferralSiteName". Shall I start from something like this in Page_Controller ?

--
if ($_GET['source'])
{echo htmlentities($_GET['source']);}
else
{echo ucwords("Standard content");}
--

Thank you!

Avatar
swaiba

Forum Moderator, 1899 Posts

20 February 2011 at 9:12pm

Edited: 20/02/2011 9:13pm

here is a basic example using an actiona nd an id through the URL...

_config.php

Director::addRules(100,array(
	'urlpage/$Action/$ID/$OtherID' => 'URLPage_Controller',
));

the page...

class URLPage extends Page {}

class URLPage_Controller extends Page_Controller {

	static $allowed_actions = array(
		'urlaction',
	);

	function urlaction() {
		$Params = $this->getURLParams();
		$id = Convert::raw2sql($Params['ID']); //the prevents against nasty - non-int stuff
		
		//do stuff using $id
		return $id;
	}
}

Avatar
mi3ll

Community Member, 24 Posts

21 February 2011 at 2:28pm

Edited: 21/02/2011 2:31pm

Here is an alternative if you do not want to add anything in your _config.php file...

class SomePage extends Page {

// Page code here 

}

class SomePage_Controller extends Page_Controller {

	function index($request) {
		// if $_GET['source'] exists, do something else, otherwise return normal page
		if($source = $request->getVar('source')) {
			
			// do stuff with $source here
			// eg. below

			return $this->customise(array(
				'Content' => 'You were redirected from: ' . $source
			));
		}
		else {
			return $this;
		}
	}
}

Avatar
gratefulsound

Community Member, 2 Posts

21 February 2011 at 9:31pm

Hi guys, thanks for the swift reply. I'll check it out today and see if I can get it to work!

Avatar
swaiba

Forum Moderator, 1899 Posts

22 February 2011 at 3:12am

Just to update my post -
I only included the _config.php to show how it normally is setup - you only need that when you have *different* parameters to those I've show... bad example I guess!