如何在PHP中获取视图中可用的所有变量?

时间:2023-01-14 14:23:52

I need to see all the variables that are available in a view. I am a front end developer so I mostly work in the views directory. I don't always know which variables are being passed to the templates by the back end dev. Instead of asking him every time an easy solution would be some type of snippet that I can temporarily paste into the view that I'm working on so I can see all the available variables and even better if I can also see their types and values.

我需要查看视图中所有可用的变量。我是前端开发人员,所以我主要在视图目录工作。我总是不知道哪些变量传递给模板的后端开发。而不是每次问他一个简单的解决方案是某种类型的片段,我可以暂时粘贴到认为我工作所以我可以看到所有可用的变量和更好的如果我还可以看到他们的类型和值。

I tried this:

我试着这样的:

<pre><?php var_dump(get_defined_vars()); ?></pre>

But since I am using Codeigniter it also shows all the other tons and tons of variables that are passed in by the framework.

但由于我使用的是Codeigniter,它还显示了框架传入的其他大量变量。

I only want to display the variables that were passed specifically from the controller that loaded the view. Is there any way to do this?

我只想显示从加载视图的控制器中具体传递的变量。有什么办法吗?

2 个解决方案

#1


50  

var_dump($this->_ci_cached_vars);

#2


-1  

One possibility could be to do something like this:

一种可能是做这样的事情:

$data['user'] = $user;
$data['cart'] = $cart;
$data['data'] = $data;

$this->load->view('view', $data);

If you did something like this, then you could always access a data array that looked the same as before it was parsed for the view.

如果您做了这样的事情,那么您总是可以访问一个数据数组,该数据数组看起来与解析视图之前的数据数组相同。

Then you could use something like print_r or whatever you wanted to take a look at the array.

然后你可以使用print_r或者任何你想要查看数组的东西。

#1


50  

var_dump($this->_ci_cached_vars);

#2


-1  

One possibility could be to do something like this:

一种可能是做这样的事情:

$data['user'] = $user;
$data['cart'] = $cart;
$data['data'] = $data;

$this->load->view('view', $data);

If you did something like this, then you could always access a data array that looked the same as before it was parsed for the view.

如果您做了这样的事情,那么您总是可以访问一个数据数组,该数据数组看起来与解析视图之前的数据数组相同。

Then you could use something like print_r or whatever you wanted to take a look at the array.

然后你可以使用print_r或者任何你想要查看数组的东西。