gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:55:44 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com