andrey-helldar/last-modified
Composer 安装命令:
composer require andrey-helldar/last-modified
包简介
Setting the response code 304 Not Modified in the absence of content changes.
README 文档
README
Last Modified for Laravel
Setting the response code 304 Not Modified in the absence of content changes.
Installation
To get the latest version of Last Modified simply require the project using Composer:
$ composer require dragon-code/last-modified
Instead, you may of course manually update your require block and run composer update if you so choose:
{
"require": {
"dragon-code/last-modified": "^4.0"
}
}
And call php artisan vendor:publish --provider="DragonCode\LastModified\ServiceProvider" command.
Note
If you were using version 2.0 or less, run the
php artisan migratecommand. If this is your first time here, then you do not need to install this command, since, starting from version 2.1, we refused to store data in the database, replacing the storage with a cache.
Next, add middleware in $middlewareGroups > web section in app/Http/Kernel.php file:
use DragonCode\LastModified\Middlewares\CheckLastModified; protected $middlewareGroups = [ 'web' => [ CheckLastModified::class, ] ]
IMPORTANT! It's recommended to add a middleware after CheckForMaintenanceMode::class.
The system works like this: when opening a page, the middleware checks if there is an entry in the database table about this link. If there is, it checks the Last-Modified header
key and returns either 200 or 304 code.
To add records to the table, it is recommended to create a console command in your application using the following example:
Create / Update
use Carbon\Carbon; use DragonCode\LastModified\Resources\Item; use DragonCode\LastModified\Services\LastModified; public function handle() { $collection_1 = Foo::whereIsActive(true)->get(); $collection_2 = Bar::where('id', '>', 50)->get(); $collection_3 = Baz::query()->get(); $builder_1 = Foo::whereIsActive(true); $builder_2 = Bar::where('id', '>', 50); $builder_3 = Baz::query(); $model_1 = Foo::whereIsActive(true)->first(); $model_2 = Bar::where('id', '>', 50)->first(); $model_3 = Baz::query()->first(); $item_1 = Item::make(['url' => 'https://example.com/foo', 'updated_at' => Carbon::now()); $item_2 = Item::make(['url' => 'https://example.com/bar', 'updated_at' => Carbon::parse('2018-03-02')); $item_3 = Item::make(['url' => 'https://example.com/baz?id=1', 'updated_at' => Carbon::now()); LastModified::make() ->collections($collection_1, $collection_2, $collection_3) ->builders($builder_1, $builder_2, $builder_3) ->models($model_1, $model_2, $model_3) ->manual($item_1, $item_2, $item_3) ->update(); }
IMPORTANT! The url attribute must be available for models.
If the model has no attribute url, it should be created.
For example:
protected getUrlAttribute(): string { $slug = $this->slug; return route('page.show', compact('slug')); }
Delete
use Carbon\Carbon; use DragonCode\LastModified\Resources\Item; use DragonCode\LastModified\Services\LastModified; public function handle() { $collection_1 = Foo::whereIsActive(false)->get(); $collection_2 = Bar::whereIn('id', [50, 60, 62, 73])->get(); $builder_1 = Foo::whereIsActive(true); $builder_2 = Bar::where('id', '>', 50); $builder_3 = Baz::query(); $model_1 = Foo::whereIsActive(false)->first(); $model_2 = Bar::whereIn('id', [50, 60, 62, 73])->first(); $item_1 = Item::make(['url' => 'https://example.com/foo', 'updated_at' => Carbon::now()]); $item_2 = Item::make(['url' => 'https://example.com/bar', 'updated_at' => Carbon::now()]); LastModified::make() ->collections($collection_1, $collection_2, $collection_3) ->builders($builder_1, $builder_2, $builder_3) ->models($model_1, $model_2, $model_3) ->manual($item_1, $item_2, $item_3) ->delete(); }
Observer
In order to reduce the load on the database and free up the crown queue, it is recommended to use the observer to update the records:
namespace App\Observers; use Illuminate\Database\Eloquent\Model; use DragonCode\LastModified\Services\LastModified; class LastModifiedObserver { public function saving(Model $model) { LastModified::make() ->models($model) ->update(); } public function deleting(Model $model) { LastModified::make() ->models($model) ->delete(); } }
Don't forget to add the link to the service provider:
namespace App\Providers; use App\Observers\LastModifiedObserver; use Illuminate\Foundation\Auth\User; use Illuminate\Support\ServiceProvider; class ObserverServiceProvider extends ServiceProvider { public function boot() { Page::observe(LastModifiedObserver::class); News::observe(LastModifiedObserver::class); // ... } }
License
This package is licensed under the MIT License.
andrey-helldar/last-modified 适用场景与选型建议
andrey-helldar/last-modified 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.62k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2019 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「seo」 「headers」 「not-modified」 「dragon」 「304」 「dragon-code」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andrey-helldar/last-modified 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andrey-helldar/last-modified 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andrey-helldar/last-modified 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Permissions-Policy (Feature-Policy) header builder and middleware for Laravel
Setting the response code 304 Not Modified in the absence of content changes.
PHP polyfill for the Apache Functions. The aim is to provides functions as the PHP way in order to get them available even if PHP is not running as an Apache module.
Help to manage meta data on Laravel Eloquent model
Helper bundle, configure the request headers from a given base URL.
Statamic Fine Seo addon
统计信息
- 总下载量: 6.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-28