這是我會怎麼處理它:
- 創建你的圖像模式的涵蓋範圍由頁,分類ECT過濾器..
- 在處理你的組件URL特性和邏輯無法查看
比方說圖庫組件:
URL:page.com/:cat /:page?
public function defineProperties()
{
return [
// Page # for pagination..
'pageNumber' => [
'title' => 'Page #',
'description' => 'Gallery Page #',
'type' => 'string',
'default' => '{{ :page }}',
],
// Category slug
'category' => [
'title' => 'Category',
'description' => 'Gallery Cat',
'type' => 'string',
'default' => '{{ :cat }}',
],
// Images to show per page
'perPage' => [
'title' => 'Images per page',
'type' => 'string',
'validationPattern' => '^[0-9]+$', // validation
'validationMessage' => 'VValidation Error',
'default' => '15',
],
// if you want to add sorting
'sortOrder' => [
'title' => 'Sort Order',
'description' => 'Images Sort Order',
'type' => 'dropdown',
'default' => 'updated_at desc'
],
];
}
public function getSortOrderOptions()
{
return Image::$allowedSortingOptions;
}
public function init()
{
$this->pageNumber = empty($this->property('pageNumber')) ? 1 : $this->property('pageNumber');
$this->perPage = $this->property('perPage');
$this->sortOrder = $this->property('sortOrder');
$this->category = $this->property('category');
}
public function onRun()
{
// here you may want to do some checks
// and add logic before querying your DB
return $this->listImages($this->pageNumber , $this->sortOrder, $this->perPage, $this->category);
}
public function listImages($pageNumber, $sortOrder, $perPage, $category){
// this is the scope you will define in your Images Model
// to handle pagination, sorting, category filtering ect..
$images = Images::listFrontEnd([
'page' => $pageNumber,
'sort' => $sortOrder,
'perPage' => $perPage,
'category' => $category,
]);
// small helper if the pagination # is > than last page
// redirect to last page..
$lastPage = $images->lastPage();
if ($this->pageNumber > $lastPage && $this->pageNumber > 1){
return Redirect::to($this->currentPageUrl(["page" => $lastPage]));
}
$this->images = $this->page['images'] = $images;
}
在你的圖片型號
:
// list the allowed sorting options that will show up in your component
public static $allowedSortingOptions = array(
'created_at asc' => 'Images Created (ascending)',
'created_at desc' => 'Images Created (descending)',
'updated_at asc' => 'Images Updated (ascending)',
// ect....
);
// Scope for your component
public function scopelistImages($query, $options)
{
/*
* Default options
*/
extract(array_merge([
'page' => 1,
'perPage' => 15,
'sort' => 'updated_at',
'category' => null
], $options));
// SORTING
if (!is_array($sort)) {
$sort = [$sort];
}
foreach ($sort as $_sort) {
if (in_array($_sort, array_keys(self::$allowedSortingOptions))) {
$parts = explode(' ', $_sort);
if (count($parts) < 2) {
array_push($parts, 'desc');
}
list($sortField, $sortDirection) = $parts;
if ($sortField == 'random') {
$sortField = Db::raw('RAND()');
}
$query->orderBy($sortField, $sortDirection);
}
}
// Filter by category
........
return $query->paginate($perPage, $page);
}
答案從RainLab Blog Plugin
不知道靈感,如果我誤解了,但是,是不是簡單的將它傳遞給之前獲取符合條件的記錄風景?像'Gallery :: where('category_id',$ catId) - > get()'。當然,您首先必須使用從參數 –
@ChristopherFrancisco獲取的名稱獲取類別ID。通過OctoberCMS,您可以拖放記錄列表或記錄詳細信息,然後設置模型類和顯示列。從那裏你可以使用Twig語法來使用記錄。我是新手,在Twig之外使用Laravel。 –
我的不好,OctoberCMS的鏈接重定向到分頁頁面,所以我認爲它只是一個分頁組件(儘管名稱說CMS,愚蠢的我哈哈哈) –