Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Create dynamic content according to URL variable

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

Page: 1
Go to End
Author Topic: 2209 Views
  • gratefulsound
    Avatar
    Community Member
    2 Posts

    Create dynamic content according to URL variable Link to this post

    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!

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Create dynamic content according to URL variable Link to this post

    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;
       }
    }

  • mi3ll
    Avatar
    Community Member
    24 Posts

    Re: Create dynamic content according to URL variable Link to this post

    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;
          }
       }
    }

  • gratefulsound
    Avatar
    Community Member
    2 Posts

    Re: Create dynamic content according to URL variable Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Create dynamic content according to URL variable Link to this post

    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!

    2209 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.