Laravel中的魔术方法 [英] Magic methods in Laravel

查看:72
本文介绍了Laravel中的魔术方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近意识到,而不是使用

So I recently became aware that instead of using

$users = User::all();
return view('home')->with('users', $users);

您能够做到

return view('home')->withUsers($users);

我相信这些被称为魔术方法"-这些文件记录在某处吗?有没有我可以找到的列表,或者是唯一的列表吗?

I believe these are known as 'magic methods' - are these documented somewhere? Is there a list I can find or is that the only one?

推荐答案

Laravel允许您出于方便的目的以这种方式传递数据,但它们不是魔术方法.魔术方法是PHP类中一组选定方法的名称,通常以双下划线 __ 开头,每个下划线都有自己的用途.魔术方法的示例是 __ call __ callStatic __ toString 等.

Laravel allows you to to pass data in that way out of convenience, but they aren't magic methods. Magic Methods is the name given to a select set of methods in PHP classes, that usually start with a double underscore __, each with their own purposes. Examples of magic methods are __call, __callStatic, __toString etc.

Laravel使用PHP的magic方法,尤其是 __ call magic方法,以提供您正在体验的功能. __ call 方法是当您尝试调用对象上不存在的方法时,由PHP调用.作为第一个参数,它接收被调用方法的名称,第二个参数,它接收作为数组传递给方法调用的参数.

Laravel uses PHP's magic methods, in particular the __call magic method, to provide the functionality that you're experiencing. The __call method is called by PHP when you try to call a method that does not exist on an object. As its first parameter it receives the name of the method that was called and the second parameter it receives the arguments that were passed to the method call as an array.

Laravel通过在 View 类上实现 __ call 方法来利用这一点.它检查开发人员调用的方法的开头是否以 with 开头,如果是,它将假定方法名称的以下部分代表您要绑定的变量的名称.呈现视图时的数据:

Laravel leverages this by implementing the __call method on the View class. It checks to see if the beginning of the method that the developer called starts with with and if so, it will assume that the following part of the method name represents the name of the variable that you want to bind data to when rendering your view:

来自 Illuminate \ View \ View :

From Illuminate\View\View:

/**
 * Dynamically bind parameters to the view.
 *
 * @param  string  $method
 * @param  array   $parameters
 * @return \Illuminate\View\View
 *
 * @throws \BadMethodCallException
 */
public function __call($method, $parameters)
{
    if (! Str::startsWith($method, 'with')) {
        throw new BadMethodCallException("Method [$method] does not exist on view.");
    }

    return $this->with(Str::camel(substr($method, 4)), $parameters[0]);
}

这提供了您正在体验的功能,并且实际上已在许多不同的地方使用了此技术,例如,在查询"构建器/雄辩的语言中,您可以通过简单地调用一个开始的方法来动态地向查询中添加where子句与 where 一起使用,并且方法调用的其余部分引用您要检查的列:

This provides the functionality that you're experiencing and this technique is in fact used in a few different places, for example, in the Query builder/Eloquent you can dynamically add where clauses to a query using by simply calling a method beginning with where and the remainder of the method call refers to the column you're checking:

User::whereEmail('foo@bar.com'); // Equal to User::where('email', 'foo@bar.com');

这篇关于Laravel中的魔术方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆