Laravel Telescope 新增 develop 額外環境

Laravel Telescope 設定 develop 環境

在安裝 local 使用 Telescope,網頁查看都沒有問題,但如果想要在測試站卻會無法執行

由於套件預設只有在 local 環境才會執行,所以需要修改部分地方才可正確的顯示

  • 新增 develop 多環境都可以使用
    app/Providers/AppServiceProvider.php
    1
    2
    3
    4
    5
    6
    7
    public function register(): void
    {
    if (in_array($this->app->environment(), ['local', 'develop'])) {
    $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
    $this->app->register(TelescopeServiceProvider::class);
    }
    }

app/Providers/TelescopeServiceProvider.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Prevent sensitive request details from being logged by Telescope.
*/
protected function hideSensitiveRequestDetails(): void
{
if (in_array($this->app->environment(), ['local', 'develop'])) {
return;
}

Telescope::hideRequestParameters(['_token']);

Telescope::hideRequestHeaders([
'cookie',
'x-csrf-token',
'x-xsrf-token',
]);
}
  • 複寫 authorization function,由於繼承的 TelescopeApplicationServiceProvider,驗應中限定環境設定只能使用 local,在不改寫vendor套件下複寫function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    // TelescopeApplicationServiceProvider 不處理
    protected function authorization()
    {
    $this->gate();

    Telescope::auth(function ($request) {
    return app()->environment('local') ||
    Gate::check('viewTelescope', [$request->user()]);
    });
    }
1
2
3
4
5
6
7
8
9
10
11
// TelescopeServiceProvider 新增 function 複寫
protected function authorization()
{
Telescope::auth(function ($request) {
if (in_array($this->app->environment(), ['local', 'develop'])) {
return true;
}

return false;
});
}