假设有这样的 Controller :
<?php
namespace App\Http\Controller
use Illuminate\Http\Request;
use Validator;
use Auth;
class MemberController extends Controller {
//some code
}
在哪里
Validator
和 Auth
属于(所以我可以看到它们的定义)它们到底是什么?我还有一个问题:here (Laravel 5.6 文档)它说
we will use the validate method provided by the Illuminate\Http\Request object.
当我检查 here如需更多信息,请联系
validate
方法!如果有人能帮助我解决这些问题,我将不胜感激。
最佳答案
Validator
和 Auth
是 Illuminate\Support\Facades
中同名 Facades 的别名.它们是类实例的静态代理。您可以在有关 Facades 的 Laravel 文档中阅读有关它们的信息。
这些别名在 config/app.php
中配置'aliases'
大批。
Laravel 5.6 Docs - Facades
对于 $request->validate(...)
:
这是一个 宏 .宏允许您向在运行时实现宏功能的类添加功能。Illuminate\Foundation\Providers\FoundationServiceProvider@registerRequestValidation
在 Illuminate\Http\Request
上设置此宏允许 validate
方法。
/**
* Register the "validate" macro on the request.
*
* @return void
*/
public function registerRequestValidation()
{
Request::macro('validate', function (array $rules, ...$params) {
return validator()->validate($this->all(), $rules, ...$params);
});
}
要快速阅读 Laravel 中的宏,请查看我的文章:
asklagbox blog - Using Macros in Laravel
关于laravel - Php/Laravel 中的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514344/