How do I call Validator from a namespace with an already existing Validator class

≡放荡痞女 提交于 2020-02-06 05:31:46

问题


I'm trying to test a function in phpspec which calls Laravel's Validator::make function (http://laravel.com/docs/4.2/validation)

However, I'm trying to call that same function from a namespace where the Validator class name is already taken. How can I call that function described in the docs?

Failed solutions:

Attempt 1

return \Illuminate\Validation\Validator::make($values,$rules);

gives me

Call to undefined method Illuminate\Validation\Validator::make()

Attempt 2

return \Illuminate\Validation\Factory::make($values,$rules);

gives me

Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory

Attempt 3

use \Validator;

gives me

Cannot declare class Isoform\Validator because the name is already in use

Attempt 4

use \Validator as DefaultValidator;

gives me

Class 'DefaultValidator' not found

回答1:


return \Illuminate\Support\Facades\Validator::make($values,$rules);

This will cause errors in phpspec, but that cannot be avoided. Although Validator::make looks like a static function - behind the scenes it is really returning an instance. Because I was using phpspec, that instance was not created hence the error.




回答2:


Try just a single forward slash to refer to the global namespace:

return \Validator::make($values,$rules);



回答3:


I solved that one. Simply delete use Validator from vendor\laravel\framework\src\Illuminate\Validation\Validator.php and then add use Validator code to your controller. Then you are ready for action.

$validator = Validator::make($request->all(), $rules);


来源:https://stackoverflow.com/questions/27973926/how-do-i-call-validator-from-a-namespace-with-an-already-existing-validator-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!