列出Laravel视图中的所有已注册变量

问题描述 投票:47回答:4

我正在使用Laravel 5.我想知道哪些变量都传递给视图内部的视图。

由于所有变量都在视图范围内,我认为我可以使用通用的PHP函数: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>

但我想知道是否有更好的方法(像View::getData()

注意: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] => 
                                                                        )
php laravel laravel-5
4个回答
70
投票

使用dd助手:

{{ dd(get_defined_vars()) }}

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

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

{{ dd(get_defined_vars()['__data']) }}

3
投票

如果您正在使用Laravel 5.1,现在允许使用自定义指令扩展Blade,您可能会觉得这很有用。您需要在此example中注册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']"
        );
    });

0
投票

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

// 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
投票

种类相同,但有点整洁:

{{dd($ __ data)}}

© www.soinside.com 2019 - 2024. All rights reserved.