I am using Laravel 5. I would like to know which are all variables passed to a view inside the view itself.

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

Something like this:

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

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

Note: get_defined_vars() deosn't work becausee it returns hundreds of useless variables (Laravel components)

这是一个使用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

update(thx,@JoeCoder):您可以通过执行以下操作进一步减少"无用的"变量:

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

Laravel相关问答推荐

Nginx上带有SSL的Laravel混响

Laravel中的策略对我不起作用,这是我的代码

Laravel Eloquent查询与集合优化

在Laravel中URL中添加保护ID

将错误消息抛出并显示为关联数组

未找到 apiResource 404 中的变量

函数接受 Laravel 集合对象,尽管只允许使用字符串

如何在生产中的 laravel 应用程序中获取标头请求值?

Laravel 保存一对多关系

Laravel belongsToMany 关系在两个表上定义本地键

Laravel 5 - 仅在特定页面/控制器(页面特定assets资源)上添加样式表

Laravel 4:读取由 javascript 设置的 cookie

SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4

如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?

如何在 Laravel 5.1 中改变环境?

如果 Laravel 中的值不为空,如何验证输入字段

无法捕获 Carbon 抛出的异常

使用 Laravel 使用 2 个磁盘复制文件

Laravel 邮件密件抄送

Laravel 更新后用户模型错误(用户类包含 3 个抽象方法)