5102 Posts in 1520 Topics by 1116 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1649 Views |
-
Create dynamic content according to URL variable

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!
-
Re: Create dynamic content according to URL variable

20 February 2011 at 9:12pm Last edited: 20 February 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;
}
} -
Re: Create dynamic content according to URL variable

21 February 2011 at 2:28pm Last edited: 21 February 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. belowreturn $this->customise(array(
'Content' => 'You were redirected from: ' . $source
));
}
else {
return $this;
}
}
} -
Re: Create dynamic content according to URL variable

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!
-
Re: Create dynamic content according to URL variable

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!
| 1649 Views | ||
|
Page:
1
|
Go to Top |



