Jump to:

23451 Posts in 18899 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Accessing GET Variables

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 3713 Views
  • inkubux
    Avatar
    Community Member
    11 Posts

    Accessing GET Variables Link to this post

    What is the best way of accessing the Get variables in my controllers. Do I have to use the $_GET suerglobal. Is the $_GET superglobal is sanitized by the framework?

    I could'nt find any documentation on this.

    Thanks for you help

  • youngmug
    Avatar
    Community Member
    19 Posts

    Re: Accessing GET Variables Link to this post

    You probably want HTTPRequest.

  • inkubux
    Avatar
    Community Member
    11 Posts

    Re: Accessing GET Variables Link to this post

    Thanks, I knew about the HTTPRequest, I just didn't knonw how to access it i a controller.

    Now I fint it $this->request.

    I feel stupid but I couldn't find it anywhere in the docs.

    Is there some docs about sapphire the framework ?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Accessing GET Variables Link to this post

    It'll be passed to the action, eg:

    function action($request) {

       $var = $request->getVar('q');

       // ... do stuff
    }

    Usually the best documentation for sapphire is http://api.silverstripe.org

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: Accessing GET Variables Link to this post

    I'm not sure if it helps but here is a snippet I picked up somewhere and adapted. It is for showing a bunch of pages on another page:

    /**
    * Get the latest n news articles, allowing us to get more
    *
    * @return DataObjectSet|false The DataObjectSet of the news articles you're looking at or boolean false if there aren't any articles
    */
    function PortfolioItems() {
    if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
    $SQL_start = (int)$_GET['start'];
    $doSet = DataObject::get(
       $callerClass = "PortfolioPage",
       $filter = "`ClassName` IN ('PortfolioPage')",
       $sort = "'Created', 'DESC'",
       $join = "",
       $limit = "{$SQL_start},5");

    return $doSet ? $doSet : false;
    }

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Accessing GET Variables Link to this post

    Just to answer the OPs question: There's nothing wrong with using the $_GET superglobal. It isn't being sanitized by the framework, so you'll have to take care of that yourself. Have a look at the Convert class, it provides some handy conversion functions.
    You could also use the HTTPRequest object, but AFAIK this isn't sanitized either.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Accessing GET Variables Link to this post

    Just FYI it is desirable to avoid directly using the $_GET superglobal as it causes all kinds of bugs and inconsistencies when requests are programatically generated rather than initiated directly by the server - such as in testing or static publishing.

    3713 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.