Skip to content

Commit a6ea241

Browse files
authored
Etag and last modified headers (#731)
1 parent fe08c2d commit a6ea241

File tree

7 files changed

+36
-3
lines changed

7 files changed

+36
-3
lines changed

src/Http/Controllers/CategoryController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public function show(int $categoryId)
1212
config(['frontend.category' => $category->only('entity_id')]);
1313
session(['latest_category_path' => $category->path]);
1414

15-
return view('rapidez::category.overview', compact('category'));
15+
$response = response()->view('rapidez::category.overview', compact('category'));
16+
17+
return $response
18+
->setEtag(md5($response->getContent() ?? ''))
19+
->setLastModified($category->updated_at);
1620
}
1721
}

src/Http/Controllers/PageController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class PageController
88
{
99
public function show(Page $page)
1010
{
11-
return view('rapidez::page.overview', compact('page'));
11+
$response = response()->view('rapidez::page.overview', compact('page'));
12+
13+
return $response
14+
->setEtag(md5($response->getContent() ?? ''))
15+
->setLastModified($page->update_time);
1216
}
1317
}

src/Http/Controllers/ProductController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function show(int $productId)
4141

4242
config(['frontend.product' => $product->only($attributes)]);
4343

44-
return view('rapidez::product.overview', compact('product'));
44+
$response = response()->view('rapidez::product.overview', compact('product'));
45+
46+
return $response
47+
->setEtag(md5($response->getContent() ?? ''))
48+
->setLastModified($product->updated_at);
4549
}
4650
}

src/Models/Category.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class Category extends Model
1212
{
1313
use HasAlternatesThroughRewrites;
1414

15+
protected $casts = [
16+
self::UPDATED_AT => 'datetime',
17+
self::CREATED_AT => 'datetime',
18+
];
19+
1520
protected $appends = ['url'];
1621

1722
protected static function booting()
@@ -32,6 +37,7 @@ protected static function booting()
3237
'children',
3338
'children_count',
3439
'position',
40+
self::UPDATED_AT,
3541
];
3642

3743
$builder

src/Models/Page.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ class Page extends Model
1111

1212
protected $primaryKey = 'page_id';
1313

14+
const CREATED_AT = 'creation_time';
15+
16+
const UPDATED_AT = 'update_time';
17+
18+
protected $casts = [
19+
self::UPDATED_AT => 'datetime',
20+
self::CREATED_AT => 'datetime',
21+
];
22+
1423
protected static function booting()
1524
{
1625
static::addGlobalScope(new IsActiveScope);

src/Models/Product.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class Product extends Model
3636

3737
protected $primaryKey = 'entity_id';
3838

39+
protected $casts = [
40+
self::UPDATED_AT => 'datetime',
41+
self::CREATED_AT => 'datetime',
42+
];
43+
3944
protected $appends = ['url'];
4045

4146
protected static function booting(): void

src/Models/Scopes/Product/WithProductAttributesScope.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function apply(Builder $builder, Model $model)
1616
$model->qualifyColumn('visibility'),
1717
$model->qualifyColumn('type_id'),
1818
$model->getQualifiedCreatedAtColumn(),
19+
$model->getQualifiedUpdatedAtColumn(),
1920
]);
2021

2122
if (empty($model->attributesToSelect)) {

0 commit comments

Comments
 (0)