Lista wszystkich zarejestrowanych zmiennych w widoku Laravel

Używam Laravel 5. Chciałbym wiedzieć, które zmienne są przekazywane do widoku wewnątrz samego widoku.

Ponieważ wszystkie zmienne są w zakresie widoku, pomyślałem, że mogę użyć generycznej funkcji PHP: get_defined_vars(); http://php.net/manual/en/function.get-defined-vars.php

Coś takiego:

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

Ale chciałbym wiedzieć, czy jest lepszy sposób (coś w rodzaju View::getData())

Notatka: get_defined_vars () deosn ' t work becausee it returns setki bezużytecznych zmiennych (składniki Laravel)

Jest to fragment (częściowy) za pomocą print_r(get_defined_vars()) (myślę, że idzie w nieskończonej pętli rekurencji):

      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] => 
                                                                        )
Author: giò, 2015-03-19

3 answers

Użyj dd helper:

{{ dd(get_defined_vars()) }}

Czytaj więcej: https://laravel.com/docs/5.4/helpers#method-dd

Update( thx, @ JoeCoder): możesz dalej wyciąć "bezużyteczne" zmienne, wykonując:

{{ dd(get_defined_vars()['__data']) }}
 52
Author: Limon Monte,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-29 16:33:42

Jeśli używasz Laravel 5.1, który teraz pozwala rozszerzyć Blade o własne dyrektywy, może się to okazać przydatne. Musisz zarejestrować dyrektywy w AppServiceProvider jak w tym przykładzie lub utworzyć własnego providera.

     /**
     * 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']"
        );
    });
 3
Author: cheelahim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-09 17:44:27

Dla lepszej czytelności i celów debugowania, można również utworzyć helper, który zamienia wyjście w tablicę.

// 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());
    }
}
 0
Author: TommyZG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-25 13:13:43