Concrete5: Can I use $_GET variable for query string on Regular Page?

痞子三分冷 提交于 2019-12-10 18:07:16

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/14613994/concrete5-can-i-use-get-variable-for-query-string-on-regular-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!