php-列出Laravel VI内的所有已注册变量

我正在使用Laravel5。我想知道在视图本身内部传递给视图的所有变量。

由于所有变量都在视图范围内,所以我认为我可以使用通用的PHP函数:print_r(get_defined_vars()) [http://php.net/manual/en/function.get-defined-vars.php]

像这样:

  // resources/view/home.blade.php
  <html>
  <body>
       <?php print_r(get_defined_vars()); ?>
  </body>
  </html>

但我想知道是否有更好的方法(例如print_r(get_defined_vars())

注意:get_defined_vars()不起作用,因为它返回数百个无用的变量(Laravel组件)

这是使用print_r(get_defined_vars())的代码段(部分)(我认为它进入了无限递归循环):

      Array
(
    [__path] => C:\net\laravel\storage\framework\views/8e030a77b0bdbacc2c4182fc04420d1d
    [__data] => Array
        (
            [__env] => Illuminate\View\Factory Object
                (
                    [engines:protected] => Illuminate\View\Engines\EngineResolver Object
                        (
                            [resolvers:protected] => Array
                                (
                                    [php] => Closure Object
                                        (
                                            [this] => Illuminate\View\ViewServiceProvider Object
                                                (
                                                    [app:protected] => Illuminate\Foundation\Application Object
                                                        (
                                                            [basePath:protected] => C:\net\laravel
                                                            [hasBeenBootstrapped:protected] => 1
                                                            [booted:protected] => 1
                                                            [bootingCallbacks:protected] => Array
                                                                (
                                                                    [0] => Closure Object
                                                                        (
                                                                            [static] => Array
                                                                                (
                                                                                    [instance] => Illuminate\Bus\BusServiceProvider Object
                                                                                        (
                                                                                            [defer:protected] => 1
                                                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                                        )

                                                                                )

                                                                            [this] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                        )

                                                                    [1] => Closure Object
                                                                        (
                                                                            [static] => Array
                                                                                (
                                                                                    [instance] => Illuminate\Translation\TranslationServiceProvider Object
                                                                                        (
                                                                                            [defer:protected] => 1
                                                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                                        )

                                                                                )

                                                                            [this] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                        )

                                                                )

                                                            [bootedCallbacks:protected] => Array
                                                                (
                                                                )

                                                            [terminatingCallbacks:protected] => Array
                                                                (
                                                                )

                                                            [serviceProviders:protected] => Array
                                                                (
                                                                    [0] => Illuminate\Events\EventServiceProvider Object
                                                                        (
                                                                            [app:protected] => Illuminate\Foundation\Application Object
 *RECURSION*
                                                                            [defer:protected] => 
                                                                        )
giò asked 2020-01-14T11:22:08Z
4个解决方案
81 votes

使用dd助手:

{{ dd(get_defined_vars()) }}

阅读更多:[https://laravel.com/docs/5.4/helpers#method-dd]

更新(thx,@ JoeCoder):您可以通过以下操作进一步削减“无用”变量:

{{ dd(get_defined_vars()['__data']) }}
Limon Monte answered 2020-01-14T11:22:38Z
3 votes

如果您使用的是Laravel 5.1,现在可以使用自定义指令扩展Blade,那么您可能会发现这很有用。 您需要像本示例一样在AppServiceProvider中注册指令,或创建自己的提供程序。

     /**
     * Blade directive to dump template variables. Accepts single parameter
     * but also could be invoked without parameters to dump all defined variables.
     * It does not stop script execution.
     * @example @d
     * @example @d(auth()->user())
     */
    Blade::directive('d', function ($data) {
        return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); ?>",
            null !== $data ? $data : "get_defined_vars()['__data']"
        );
    });

    /**
     * Blade directive to dump template variables. Accepts single parameter
     * but also could be invoked without parameters to dump all defined variables.
     * It works similar to dd() function and does stop script execution.
     * @example @dd
     * @example @dd(auth()->user())
     */
    Blade::directive('dd', function ($data) {
        return sprintf("<?php (new Illuminate\Support\Debug\Dumper)->dump(%s); exit; ?>",
            null !== $data ? $data : "get_defined_vars()['__data']"
        );
    });
cheelahim answered 2020-01-14T11:22:59Z
2 votes

有点一样,但是有点整洁:

{{dd($ __ data)}}

Leon answered 2020-01-14T11:23:23Z
0 votes

为了更好的可读性和调试目的,您还可以创建一个将输出转换为数组的帮助器。

// as per comment from Braunson add this to custom helpers function in app\helpers.php and include it via composer.

if (! function_exists('da')) {
    /**
     * Dump the passed variables to array and end the script.
     *
     * @param  mixed
     * @return void
     */
    function da()
    {
        array_map(function ($x) {
            dd($x->toArray());
        }, func_get_args());
    }
}
TommyZG answered 2020-01-14T11:23:43Z
translate from https://stackoverflow.com:/questions/29146879/list-all-registered-variables-inside-a-laravel-view