2015-05-09 77 views
3

我正在創建一個電子商務系統。我在控制器中有一個getCategories()函數。這個函數可以工作,但我試圖擴展它的功能,以便它能根據過濾器返回適當的視圖。Laravel 4 - 過濾結果和緩存

可以說用戶正在查看特定類別。每個類別都包含產品,我試圖根據品牌爲產品創建一個過濾器。例如,如果用戶屬於類別'吉他',他應該能夠基於現有品牌'Gibson'來過濾其產品。

所以我實現了上述場景,但我不確定這是否非常有效。我創建了一個鏈接:

@foreach($brand as $key => $value) 
    <a href="/store/categories/{{$category->slug}}?brand={{$value->slug}}">{{$value->name}}</a> 
@foreach 

正如你可以看到我傳遞的參數brand通過URL而這個鏈接調用getCategories()功能,在該功能中,我檢查,如果該鏈接包含像這樣的參數:

if (Input::has('brand')) { 
    $brandflag = true; 
    $brand_input = Input::get('brand'); 
    $brandfilter = array('type'=>'Brand', 'name' => ucfirst($brand_input)); 
} 

$brandflag初始設置爲false,如果它存在,它改變其價值true。如果$brandflag更改爲true,也是另一個if將不同的數據返回到視圖。

if ($brandflag == true) { 
    $b = Brand::whereSlug($brand_input)->first(); 
    $products = Product::where('brand_id','=', $b->id)->whereIn('category_id', $children->lists('id')); 
    return View::make('store.categories') 
     ->with('products', $products->paginate(6)) 
     ->with('ranges', $ranges) 
     ->with('brandfilter', $brandfilter) 
     ->with('category', $main) 
     ->with('brands', $brands) 
     ->with('children', $children_array) 
     ->with('seo', $seo); 
} 

以上所有作品,但是當我緩存的這個類別的路線沒有人會工作,因爲它會緩存視圖,它會指的是緩存的文件。在?之後通過的任何內容都會被忽略。

Route::get('categories/{slug}', array(
    'as' => 'store.categories', 
    'uses' => '[email protected]' 
))->where('slug', '(.*)?')->before('cache.fetch')->after('cache.put'); 

我應該如何清理/修復我的代碼來獲得此功能工作,能夠緩存的類別?

+0

我只想發表評論,因爲我不是100%確定。我認爲這不是一個明智的緩存頁面。這個頁面有機會不斷變化,因爲這不是一個理想的緩存點。您想要緩存不經常更改的頁面,並且不接受許多參數。這就是緩存意味着什麼。 –

回答

2

我假設cache.putcache.fetch過濾器基於this article

在他的示例中,作者使用$request->url()來構建緩存鍵。 API docs表示此方法不包含查詢字符串。

爲了緩存與你必須調用與任何fullUrl(),或url()query()組合替換到url()查詢參數工作。

+0

太棒了!把它改成'fullUrl()'就行了! – cch