List all registered variables inside a Laravel view(列出 Laravel 视图中的所有注册变量)
问题描述
我正在使用 Laravel 5.我想知道哪些是传递给视图本身内部视图的所有变量.
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
Since all variables are in the view scope I thought I could use the generic PHP function: 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()
)
But I would like to know if there is a better way (something like View::getData()
)
注意: get_defined_vars() 不起作用,因为它返回了数百个无用的变量(Laravel 组件)
Note: get_defined_vars() deosn't work becausee it returns hundreds of useless variables (Laravel components)
这是一个使用 print_r(get_defined_vars())
的片段(部分)(我认为它进入无限递归循环):
This is a snippet (partial) using print_r(get_defined_vars())
(i think it goes in infinite recursion loop):
Array
(
[__path] => C:
etlaravelstorageframeworkviews/8e030a77b0bdbacc2c4182fc04420d1d
[__data] => Array
(
[__env] => IlluminateViewFactory Object
(
[engines:protected] => IlluminateViewEnginesEngineResolver Object
(
[resolvers:protected] => Array
(
[php] => Closure Object
(
[this] => IlluminateViewViewServiceProvider Object
(
[app:protected] => IlluminateFoundationApplication Object
(
[basePath:protected] => C:
etlaravel
[hasBeenBootstrapped:protected] => 1
[booted:protected] => 1
[bootingCallbacks:protected] => Array
(
[0] => Closure Object
(
[static] => Array
(
[instance] => IlluminateBusBusServiceProvider Object
(
[defer:protected] => 1
[app:protected] => IlluminateFoundationApplication Object
*RECURSION*
)
)
[this] => IlluminateFoundationApplication Object
*RECURSION*
)
[1] => Closure Object
(
[static] => Array
(
[instance] => IlluminateTranslationTranslationServiceProvider Object
(
[defer:protected] => 1
[app:protected] => IlluminateFoundationApplication Object
*RECURSION*
)
)
[this] => IlluminateFoundationApplication Object
*RECURSION*
)
)
[bootedCallbacks:protected] => Array
(
)
[terminatingCallbacks:protected] => Array
(
)
[serviceProviders:protected] => Array
(
[0] => IlluminateEventsEventServiceProvider Object
(
[app:protected] => IlluminateFoundationApplication Object
*RECURSION*
[defer:protected] =>
)
推荐答案
使用 dd
助手:
{{ dd(get_defined_vars()) }}
阅读更多:https://laravel.com/docs/5.4/helpers#method-dd
更新(thx,@JoeCoder):您可以通过执行以下操作进一步减少无用"变量:
Update (thx, @JoeCoder): you can further cutdown on the "useless" variables by doing:
{{ dd(get_defined_vars()['__data']) }}
这篇关于列出 Laravel 视图中的所有注册变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:列出 Laravel 视图中的所有注册变量
基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01