dragon-code/last-modified

在内容未更改的情况下设置响应代码304未修改。

4.2.0 2024-03-13 08:45 UTC

README

Laravel Last Modified

Stable Version Unstable Version Total Downloads Github Workflow Status License

在内容未更改的情况下设置响应代码304未修改。

安装

要获取Latest Modified的最新版本,只需使用Composer安装该项目。

$ composer require dragon-code/last-modified

当然,您也可以手动更新require块并运行composer update

{
    "require": {
        "dragon-code/last-modified": "^4.0"
    }
}

然后,调用php artisan vendor:publish --provider="DragonCode\LastModified\ServiceProvider"命令。

注意

如果您之前使用的是2.0或更低版本,请运行php artisan migrate命令。如果您是第一次使用,则不需要安装此命令,因为从版本2.1开始,我们拒绝将数据存储在数据库中,用缓存替换存储。

接下来,在app/Http/Kernel.php文件中的$middlewareGroups > web部分添加中间件。

use DragonCode\LastModified\Middlewares\CheckLastModified;

protected $middlewareGroups = [
    'web' => [
         CheckLastModified::class,
    ]
]

重要!建议在CheckForMaintenanceMode::class之后添加中间件。

系统工作方式如下:打开页面时,中间件检查数据库表中是否有关此链接的条目。如果有,则检查Last-Modified头键并返回200或304代码。

要向表中添加记录,建议在应用程序中使用以下示例创建控制台命令

创建/更新

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

重要!模型的url属性必须可用。

如果模型没有url属性,则应创建它。

例如

protected getUrlAttribute(): string
{
    $slug = $this->slug;

    return route('page.show', compact('slug'));
}

删除

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

观察者

为了减轻数据库负载并释放主队列,建议使用观察者更新记录

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

别忘了添加到服务提供者的链接

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

        // ...
    }
}

许可证

此包根据MIT许可证授权。