【问题标题】:Laravel 4 - Filter results and cachingLaravel 4 - 过滤结果和缓存
【发布时间】:2015-07-20 07:42:07
【问题描述】:

我正在创建一个电子商务系统。我在控制器中有一个getCategories() 函数。此功能有效,但我尝试扩展其功能,以便它根据过滤器返回适当的视图。

假设用户正在查看特定类别。每个类别都包含产品,我尝试根据产品的品牌为其创建过滤器。例如,如果用户属于 '吉他' 类别,他应该能够根据现有品牌 'Gibson' 过滤其产品。

所以我实现了上述场景,但我不确定这是否非常有效。我创建了一个链接:

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

如您所见,我正在通过 url 传递参数 brand,并且此链接调用 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' => 'StoreController@getCategories'
))->where('slug', '(.*)?')->before('cache.fetch')->after('cache.put');

我应该如何清理/修复我的代码以使此功能正常工作并能够缓存类别?

【问题讨论】:

  • 我只是想发表评论,因为我不是 100% 确定。我认为这不是一个明智的缓存页面。此页面有机会不断更改,因此它不是一个理想的缓存点。您希望缓存不经常更改且不接受很多参数的页面。这就是缓存的意义所在。

标签: php laravel caching routes


【解决方案1】:

我假设cache.putcache.fetch 过滤器基于this article

在他的示例中,作者使用$request-&gt;url() 来构建缓存键。 API docs 表示此方法不包含查询字符串。

为了使缓存与查询参数一起使用,您必须将对 url() 的调用替换为 fullUrl()url()query() 的组合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2018-07-29
    • 2011-09-19
    • 2019-11-11
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    相关资源
    最近更新 更多