laravel 视图中的 e() 方法是做什么用的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33075158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:29:06  来源:igfitidea点击:

What is the e() method in laravel views for?

phplaravellaravel-5

提问by Rohan

I was digging through laravel and I went through how the blade views are interpreted and what I came across was that:

我正在挖掘 laravel 并了解了刀片视图的解释方式,我遇到的是:

This:

这个:

{{ $tenant->name }}

Translates to this:

翻译成这样:

<?php echo e($tenant->name); ?>

I don't understand what the e()method is for? I could not find it on the php.net too so I am guessing it is a part of laravel 5 itself. But what does it do?

不明白这个e()方法是干什么用的?我也无法在 php.net 上找到它,所以我猜它是 Laravel 5 本身的一部分。但是它有什么作用呢?

回答by Glad To Help

from the docs:

从文档:

e()

e()

The e function runs htmlentities over the given string:

e 函数在给定的字符串上运行 htmlentities:

echo e('<html>foo</html>');

// &lt;html&gt;foo&lt;/html&gt;

http://laravel.com/docs/5.1/helpers#method-e

http://laravel.com/docs/5.1/helpers#method-e

回答by Kalhan.Toress

say your going to print some data from the database on a web page, or going to put in to the database as a input like,

假设您要在网页上打印数据库中的一些数据,或者将作为输入放入数据库,例如,

{{ $tenant->name }}

and think value of $tenant->nameis something like

并认为 value of$tenant->name是这样的

<script>
    alert("Errors....");
</script>

after rendering this in the browser you will get an alert. This is an security issue so we need to avoid from rendering those content and we don't need these kind of data in out database.

在浏览器中渲染后,您将获得一个alert. 这是一个安全问题,因此我们需要避免呈现这些内容,并且我们不需要在数据库中使用这些类型的数据。

so we need to sanitize those data

所以我们需要清理这些数据

to do that laravel provides some options

为此,laravel 提供了一些选项

HTML::entities($tenant->name);

HTML::entities($tenant->name);

and e()is and helper function to HTML::entities

并且e()是和辅助函数HTML::entities

and you can get the same behavior by using

并且您可以通过使用获得相同的行为

e($tenant->name);

e($tenant->name);

if $tenant->nameis <script>alert("Errors....");</script>then after applying to e()you will get something below,

如果$tenant->name<script>alert("Errors....");</script>那么在申请之后e()你会得到下面的东西,

"&lt;script&gt; alert(&quot;Errors....&quot;); &lt;/script&gt;"

"&lt;script&gt; alert(&quot;Errors....&quot;); &lt;/script&gt;"

this is no longer process as a script

这不再作为脚本处理

here is a good recipe

这是一个很好的食谱

ORthere is a easy way to do this

或者有一种简单的方法可以做到这一点

use triple curly braces {{{ }}}instead of double braces {{ }}this will also sanitize the content.

使用三重花括号{{{ }}}代替双大括号,{{ }}这也将清理内容。