百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-server-timing

> 编程语言
开源

在 Laravel 应用中添加 Server-Timing 头信息。

571 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 Laravel 应用中添加 Server-Timing 头信息。

Laravel Server Timings

Add Server-Timing header information from within your Laravel apps.

Installation

You can install the package via composer:

bash
composer require beyondcode/laravel-server-timing

Usage

To add server-timing header information, you need to add the \BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class, middleware to your HTTP Kernel. In order to get the most accurate results, put the middleware as the first one to load in the middleware stack.

Laravel 11

bootstrap/app.php

php
return Application::configure(basePath: dirname(__DIR__))
    // ...
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->prepend(\BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class);
    })
    // ...
    ->create();

Laravel 10 and below

app/Http/Kernel.php

php
class Kernel extends HttpKernel
{
    protected $middleware = [
        \BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class,
        // ...
    ];

By default, the middleware measures only three things, to keep it as light-weight as possible:

  • Bootstrap (time before the middleware gets called)
  • Application time (time to get a response within the app)
  • Total (total time before sending out the response)

Once the package is successfully installed, you can see your timing information in the developer tools of your browser. Here's an example from Chrome:

Adding additional measurements

If you want to provide additional measurements, you can use the start and stop methods. If you do not explicitly stop a measured event, the event will automatically be stopped once the middleware receives your response. This can be useful if you want to measure the time your Blade views take to compile.

php
use BeyondCode\ServerTiming\Facades\ServerTiming;

ServerTiming::start('Running expensive task');

// Take a nap
sleep(5);

ServerTiming::stop('Running expensive task');

If you already know the exact time that you want to set as the measured time, you can use the setDuration method. The duration should be set as milliseconds:

php
ServerTiming::setDuration('Running expensive task', 1200);

In addition to providing milliseconds as the duration, you can also pass a callable that will be measured instead:

php
ServerTiming::setDuration('Running expensive task', function() {
    sleep(5);
});

Adding textual information

You can also use the Server-Timing middleware to only set textual information without providing a duration.

php
ServerTiming::addMetric('User: '.$user->id);

Publishing configuration file

The configuration file could be published using: php artisan vendor:publish --tag=server-timing-config

You can disable the middleware by changing the timing.enabled configuration to false or adding SERVER_TIMING_ENABLED=false to your .env file.

Testing

bash
composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

  • Marcel Pociot
  • All Contributors

License

The MIT License (MIT). Please see License File for more information.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言