如何只向某些人显示laravel debugbar?

时间:2018-07-24 04:14:34

标签: php laravel laravel-5

我使用barryvdh / laravel-debugbar。如何只为某些人显示调试栏?

3 个答案:

答案 0 :(得分:2)

您可以检查IP地址以在web.php中进行切换。

//Enabling DEBUGBAR in Production Only for developers
if(in_array(Request::ip(), [allowed IPs])) {
    config(['app.debug' => true]);
}

答案 1 :(得分:1)

您可以在AppServiceProvider.php

中执行此操作
namespace App\Providers;
use Barryvdh\Debugbar\ServiceProvider as Bry;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {


    }
   public function register(){

      if($_SERVER['REMOTE_ADDR'] == 'YourIp'){
         $this->app->register(Bry::class); // or $this->app->register(new  Barryvdh\Debugbar\ServiceProvider()); 
       }

   }

通过这种方式,您可以动态注册ServiceProvider。

希望这会有所帮助。

答案 2 :(得分:1)

在此处查看文档:{​​{3}}

如果要在运行时启用/禁用调试栏,请使用以下代码:

\Debugbar::enable();
\Debugbar::disable();

您可以这样做。创建一个中间件php artisan make:middleware TestMiddleware,不要忘记编辑app/Http/Kernel.php并添加TestMiddleware

protected $middleware = [
        ...
        \App\Http\Middleware\TestMiddleware::class,
        ...
    ];

然后这就是您的TestMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;

class TestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->user() && in_array(auth()->id(), [1,2,3])) {
            \Debugbar::enable();
        }
        else {
            \Debugbar::disable();
        }

        return $next($request);
    }
}