3
votes

I am creating an ecommerce system. I have a getCategories() function in a controller. This function works but I tried to extend its functionality so that it would return the appropriate view according to a filter.

Lets say the user is viewing a specific category. Each category contains products and I tried to create a filter for the products based on their brand. For example if the user is in the category 'Guitars' he should be able to filter its products based on an existing brand 'Gibson'.

So I implemented the above scenario but I am not sure if that's very efficient. I've created a link:

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

As you can see I am passing the parameter brand through the url and this link calls the getCategories() function, where inside that function I am checking if the link contains the parameter like so:

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

$brandflag is initially set to false and if it exists it changes its value to true. Also another if to return a different data to the view if the $brandflag changed to true.

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);
}

All of the above works, however when I am caching the categories route none of this would work because it would cache the view and it will refer to that cached file. Anything passed after the ? gets ignored.

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

How should I clean up/fix my code to get this functionality working and be able to cache the categories?

1
I just want to make a comment cause I'm not 100% sure. I don't think this would be a wise page to cache. This page has the opportunity to change constantly and since that is the case it is not an ideal caching point. You want to cache pages that don't change often and does not accept many parameters. That is more what caching was meant for.Cayce K

1 Answers

3
votes

I am assuming that the cache.put and cache.fetch filters are based on this article.

In his example, the author uses $request->url() to build the cache key. The API docs state that this method does not include the query string.

In order for the caching to work with the query parameters you'll have to replace the call to url() with either fullUrl(), or a combination of url() and query().