Php 如何从已存在的验证器类的命名空间调用验证器
我试图在phpspec中测试一个函数,它调用了Laravel的Php 如何从已存在的验证器类的命名空间调用验证器,php,laravel,laravel-4,phpspec,psr-4,Php,Laravel,Laravel 4,Phpspec,Psr 4,我试图在phpspec中测试一个函数,它调用了Laravel的Validator::make函数() 但是,我试图从一个名称空间调用同一个函数,在这个名称空间中,Validator类名已经被使用。如何调用文档中描述的函数 失败的解决方案: 尝试1 return \Illuminate\Validation\Validator::make($values,$rules); 给我 Call to undefined method Illuminate\Validation\Validator::m
Validator::make
函数()
但是,我试图从一个名称空间调用同一个函数,在这个名称空间中,Validator
类名已经被使用。如何调用文档中描述的函数
失败的解决方案:
尝试1
return \Illuminate\Validation\Validator::make($values,$rules);
给我
Call to undefined method Illuminate\Validation\Validator::make()
Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory
Cannot declare class Isoform\Validator because the name is already in use
Class 'DefaultValidator' not found
尝试2
return \Illuminate\Validation\Factory::make($values,$rules);
给我
Call to undefined method Illuminate\Validation\Validator::make()
Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory
Cannot declare class Isoform\Validator because the name is already in use
Class 'DefaultValidator' not found
尝试3次
use \Validator;
给我
Call to undefined method Illuminate\Validation\Validator::make()
Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory
Cannot declare class Isoform\Validator because the name is already in use
Class 'DefaultValidator' not found
尝试4次
use \Validator as DefaultValidator;
给我
Call to undefined method Illuminate\Validation\Validator::make()
Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory
Cannot declare class Isoform\Validator because the name is already in use
Class 'DefaultValidator' not found
请尝试使用单个正斜杠来引用全局命名空间:
return \Validator::make($values,$rules);
这将导致phpspec中出现错误,但无法避免。尽管
Validator::make
看起来像一个静态函数,但在幕后它实际上是在返回一个实例。因为我使用的是phpspec,所以没有创建该实例,因此出现了错误。我解决了这个问题。只需从vendor\laravel\framework\src\light\Validation\Validator.php
中删除use Validator
,然后将use Validator
代码添加到控制器中。那你就准备好行动了
$validator = Validator::make($request->all(), $rules);
可能的提示:“调用未定义的方法”表示它确实在该位置找到了一个类,但它可能不是您要查找的类。@mopo922如果我有错误的类,那么正确的类在哪里?您可以使用
\validator::make($values,$rules)获取Laravel的验证器
@user3158900返回的类“Validator”在尝试4中未找到
,那么DefaultValidator
来自哪里?如果您还添加了use\DefaultValidator
,那该怎么办呢?谢谢mopo,但是它在第92行的/vendor/laravel/framework/src/illighte/Validation/Factory.php中不在对象上下文中时使用$this返回<代码>工厂
有一个make
功能,但它不是静态的nada:未找到类“Validator”
。正在做外观的研究,所以谢谢你的指点。这让人费解。您正在使用“其他”验证器类吗?如果没有,可能使用\验证程序在你的文件顶部的代码>会起作用吗?确实如此。我的命名空间有一个验证器
类,但我正在尝试访问文档中的类,而不重命名我的当前类。use\Validator
和use\Validator as DefaultValidator
仍然不起作用(编辑了原始问题并说明了原因)