php - 列出 Laravel View 中的所有注册变量

标签 php laravel laravel-5

我正在使用 Laravel 5。我想知道哪些是传递给 View 本身的 View 的所有变量。

由于所有变量都在 View 范围内,我想我可以使用通用 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] => 
                                                                        )

最佳答案

使用 dd 助手:

{{ dd(get_defined_vars()) }}

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

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

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

关于php - 列出 Laravel View 中的所有注册变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29146879/

相关文章:

php - Laravel 5 中的委托(delegate)路由保护 - 首先检查 Auth

php - 在 mysql (php) 中存储缺少日期/月份的日期的方法

php - Laravel/00webhost 错误 404. 在此服务器上找不到请求的 URL

php - 在 Laravel Forge 上部署抛出 faker not found Exception

laravel - 我的服务器只加载纯 HTML,没有 css 或 js 文件

php - 绕过 max_execution_time?

php - 在 Laravel 中找不到类 'Predis\Client'

mysql - MySQL 数据库事务是否会破坏使用 RefreshDatabase 或 DatabaseTransactions 的 Laravel PHPUnit 测试?

laravel - 如何删除所有排队的作业,因为它会导致错误?

php - 在 Laravel 中执行连接查询的更好方法