Laravel 4 - 过滤结果和缓存
Laravel 4 - Filter results and caching
我正在创建一个电子商务系统。我在控制器中有一个 getCategories()
函数。此功能有效,但我试图扩展其功能,以便它 return 根据过滤器显示适当的视图。
假设用户正在查看特定类别。每个类别都包含产品,我试图根据产品的品牌为产品创建过滤器。例如,如果用户属于 'Guitars' 类别,他应该能够根据现有品牌 'Gibson'[=42 过滤其产品=].
所以我实现了上面的场景,但我不确定这是否非常有效。我创建了一个 link:
@foreach($brand as $key => $value)
<a href="/store/categories/{{$category->slug}}?brand={{$value->slug}}">{{$value->name}}</a>
@foreach
如您所见,我正在通过 url 传递参数 brand
并且此 link 调用 getCategories()
函数,在该函数内部我正在检查是否link 包含如下参数:
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
到 return 视图的不同数据
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);
}
以上所有方法都有效,但是当我缓存类别路由时,none 会起作用,因为它会缓存视图并且它将引用该缓存文件。 ?
之后传递的任何内容都将被忽略。
Route::get('categories/{slug}', array(
'as' => 'store.categories',
'uses' => 'StoreController@getCategories'
))->where('slug', '(.*)?')->before('cache.fetch')->after('cache.put');
我应该如何清理 up/fix 我的代码才能使此功能正常工作并能够缓存类别?
我假设 cache.put
和 cache.fetch
过滤器基于 this article。
在他的例子中,作者使用$request->url()
来构建缓存键。 API docs 声明此方法不包含查询字符串。
为了使缓存与查询参数一起使用,您必须将对 url()
的调用替换为 fullUrl()
或 url()
和 [= 的组合16=].
我正在创建一个电子商务系统。我在控制器中有一个 getCategories()
函数。此功能有效,但我试图扩展其功能,以便它 return 根据过滤器显示适当的视图。
假设用户正在查看特定类别。每个类别都包含产品,我试图根据产品的品牌为产品创建过滤器。例如,如果用户属于 'Guitars' 类别,他应该能够根据现有品牌 'Gibson'[=42 过滤其产品=].
所以我实现了上面的场景,但我不确定这是否非常有效。我创建了一个 link:
@foreach($brand as $key => $value)
<a href="/store/categories/{{$category->slug}}?brand={{$value->slug}}">{{$value->name}}</a>
@foreach
如您所见,我正在通过 url 传递参数 brand
并且此 link 调用 getCategories()
函数,在该函数内部我正在检查是否link 包含如下参数:
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
到 return 视图的不同数据
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);
}
以上所有方法都有效,但是当我缓存类别路由时,none 会起作用,因为它会缓存视图并且它将引用该缓存文件。 ?
之后传递的任何内容都将被忽略。
Route::get('categories/{slug}', array(
'as' => 'store.categories',
'uses' => 'StoreController@getCategories'
))->where('slug', '(.*)?')->before('cache.fetch')->after('cache.put');
我应该如何清理 up/fix 我的代码才能使此功能正常工作并能够缓存类别?
我假设 cache.put
和 cache.fetch
过滤器基于 this article。
在他的例子中,作者使用$request->url()
来构建缓存键。 API docs 声明此方法不包含查询字符串。
为了使缓存与查询参数一起使用,您必须将对 url()
的调用替换为 fullUrl()
或 url()
和 [= 的组合16=].