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.

Customising the CMS

Create dynamic content according to URL variable


Reply

5 Posts   2238 Views

Avatar
gratefulsound

19 February 2011 at 5:04am Community Member, 2 Posts

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

20 February 2011 at 9:12pm (Last edited: 20 February 2011 9:13pm), Forum Moderator, 1796 Posts

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

21 February 2011 at 2:28pm (Last edited: 21 February 2011 2:31pm), Community Member, 24 Posts

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

21 February 2011 at 9:31pm Community Member, 2 Posts

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

Avatar
swaiba

22 February 2011 at 3:12am Forum Moderator, 1796 Posts

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!