Php 如何获得无正面防护(无静电)
使用静态上下文(facades),以下代码起作用:Php 如何获得无正面防护(无静电),php,laravel,laravel-4,non-static,Php,Laravel,Laravel 4,Non Static,使用静态上下文(facades),以下代码起作用: $result = Auth::attempt(Input::only('email', 'password')); 假设我想将静态上下文减少到最小(这在Laravel中是可能的) 我正在做一个小小的妥协,并获得对该应用程序的引用: /* @var $app Illuminate\Foundation\Application */ $app = App::make("app"); 。。。然后获取身份验证管理器: /* @var $auth
$result = Auth::attempt(Input::only('email', 'password'));
假设我想将静态上下文减少到最小(这在Laravel中是可能的)
我正在做一个小小的妥协,并获得对该应用程序的引用:
/* @var $app Illuminate\Foundation\Application */
$app = App::make("app");
。。。然后获取身份验证管理器:
/* @var $auth \Illuminate\Auth\AuthManager */
$auth = $app->get("auth");
现在的问题是:
AuthManager
没有尝试
方法<代码>防护装置不起作用。唯一的问题是:卫兵在国际奥委会成员中没有约束力。那么如何获得它呢?您可以使用依赖项注入来获得它
use Illuminate\Auth\Guard as Auth;
public $auth;
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
public function doSomething()
{
$this->auth->attempt(Input::only('email', 'password'));
}
另外,p.s.
Guard
不是一个静态引用,它是一个在引用时创建实例的外观。因此,您仍然可以测试等,但这是另一次的讨论:)AuthManager
从Manager
继承了driver()
方法,该方法将提供一个驱动程序实例(显然是保护)
此外,Manager还使用magic将对不存在函数的任何调用转发给驱动程序:
public function __call($method, $parameters)
{
return call_user_func_array(array($this->driver(), $method), $parameters);
}
所以,回答我自己的问题:
/* @var $manager \Illuminate\Auth\AuthManager */
$manager = $app->get("auth");
/* @var $guard \Illuminate\Auth\Guard */
$guard = $manager->driver();
。。。但是,当然,接口不能保证你得到的是类似于警卫的东西。希望一切顺利。我需要路由器中的一些魔法才能让它工作吗?如果我将参数添加到构造函数中,我会得到带有消息“Class Illumb\Contracts\Auth\Guard不存在”的“ReflectionException”,如果我将参数添加到控制器方法中,我会得到传递给RpcController::get()的参数1必须是Illumb\Contracts\Auth\Guard的实例,没有给出任何参数(我使用的是Laravel 4).Whops-抱歉-我想的是Laravel 5,我记不起它是
使用illible\Auth\AuthManager作为$Auth
还是使用illible\Auth\Guard作为Auth
。在Auth编译时,请同时尝试这两种方法。use illumb\Auth\Guard,但我得到的消息是,illumb\Container\BindingResolutionException'目标[illumb\Auth\UserProviderInterface]不可实例化。
AuthManager工作。但我真正的问题是从经理那里得到警卫