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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Accessing GET Variables


Go to End


7 Posts   7194 Views

Avatar
inkubux

Community Member, 11 Posts

6 November 2009 at 4:26pm

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

Avatar
youngmug

Community Member, 19 Posts

6 November 2009 at 5:12pm

You probably want HTTPRequest.

Avatar
inkubux

Community Member, 11 Posts

7 November 2009 at 4:21pm

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 ?

Avatar
Hamish

Community Member, 712 Posts

8 November 2009 at 10:34pm

Edited: 08/11/2009 10:34pm

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

Avatar
yurigoul

Community Member, 203 Posts

8 November 2009 at 10:38pm

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

Avatar
bummzack

Community Member, 904 Posts

8 November 2009 at 10:46pm

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.

Avatar
ajshort

Community Member, 244 Posts

8 November 2009 at 11:02pm

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.