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   2313 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, 1804 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, 1804 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!