4

How does $_GET Variable works with Concrete5? Can I use that on regular page?

I know I can do this with single page via url segment, I'm just wondering if it is possible with regular page.

Example is :http://www.domain_name.com/about-us/?name=test...

dedano
  • 51
  • 3

2 Answers2

2

Get-parameters are available via the controllers. In the view of a page or block use:

$this->controller->get("parameterName");

A cleaner way for custom parameters would be to define them in the function view() of the page controller. If at http://www.domain_name.com/about-us is your page and you define the view function of it's pagetype controller like this:

function view($name) {
    $this->set("name", $name);
}

... and call the URL http://www.domain_name.com/about-us/test – then "test" will be passed under $name to your page view.

Note that controllers for page types must be in controllers/page_types/ and called BlablaPageTypeController ... with "PageType" literally being in there.

johjoh
  • 464
  • 4
  • 18
0

You can use it in a template. For instance, you can grab a variable...

$sort_by = $_GET['sort'];

And then use that variable in a PageList lookup, similar to:

$pl = new PageList();  
$ctHandle = "teaching";
// Available Filters
$pl->filterByCollectionTypeHandle($ctHandle); //Filters by page type handles.
// Sorting Options

if ($sort_by == "name") {
   $pl->sortByName();
} else {
   $pl->sortBy('teaching_date', 'desc'); // Order by a page attribute
}

// Get the page List Results 
$pages = $pl->getPage(); //Get all pages that match filter/sort criteria.
$pages = $pl->get($itemsToGet = 100, $offset = 0);

Then you can iterate over that array to print stuff out...eg

if ($pages) {
  foreach ($pages as $page){
echo '<a href="'.$page->getCollectionPath().'">'.$page->getCollectionName() . '</a><br />';
  }
}

Props to the C5 Cheatsheet for the PageList code.

tofraser
  • 106
  • 1
  • 8
  • Thank you @tofraser, what about on custom block view? like used the $_GET value to set a condition? It is working on Edit mode but when I logged out, it doesnt work. I retrieved the GET value on my view. – dedano Jan 30 '13 at 22:22
  • I don't think I've tried to grab a variable in a custom block view. If I have a chance later on, I will see what results I get. – tofraser Jan 30 '13 at 22:26
  • The $_GET array seems to be always available for me, no matter if I'm logged in or not. Maybe a configuration or permission issue. – johjoh Feb 01 '13 at 01:53
  • the $_GET certainly has nothing to do wheter you are logged in or not! – Nukey Aug 06 '14 at 11:54
  • It matters, dont know where to change it – Ganesh RJ Jul 30 '18 at 11:11