Laravel 5 laravel 5如何知道我要使用哪种合同实现?
我对如何使用合同有点困惑。我认为这是因为我没有使用单元测试,所以我不清楚契约是如何工作的 让我们看看这段代码:Laravel 5 laravel 5如何知道我要使用哪种合同实现?,laravel-5,Laravel 5,我对如何使用合同有点困惑。我认为这是因为我没有使用单元测试,所以我不清楚契约是如何工作的 让我们看看这段代码: use Illuminate\Contracts\Auth\Guard; ... public function __construct(Guard $auth) { $this->auth = $auth; $this->middleware('guest', ['except' => 'getLogout']); } public func
use Illuminate\Contracts\Auth\Guard;
...
public function __construct(Guard $auth)
{
$this->auth = $auth;
$this->middleware('guest', ['except' => 'getLogout']);
}
public function postRegister(RegisterRequest $request)
{
// Registration form is valid, create user...
$this->auth->login($user);
return redirect('/');
}
login
契约方法:$this->auth->login($user)
?如果我想用我自己的,我怎么能改变课程Auth::user()
作为一个例子,我在任何控制器中的任何地方都使用它,它都能工作。现在,我应该在控制器方法中插入一个契约,并像$auth->user
那样使用它on()
和off()
,我有多种方法可以做到这一点。我需要为此创建新合同吗我希望我能让你明白一点 公元1年。您可以在第792行附近的
/vendor/laravel/framework/src/illumb/Foundation/Application.php检查默认绑定(methodregistercorecontaineralias
)。如果您想创建自己的类或扩展现有类,我建议您查看或(这篇文章更多地介绍了Laravel4.x,但可能会给您一个想法)
公元2年。实际上,您仍然可以使用Auth::user(),但我在构造函数或方法中插入了一个约定,并像$this->Auth->user或$Auth->user那样调用它
公元3年。我有一个/app/Repositories
文件夹,在那里我放置了我的接口和实现,所以按照您的示例,我将创建子文件夹Lamp
,并使用打开()
和关闭()
方法创建LampInterface
,然后我将创建类似Lamp.php的东西,实现LampInterface
。接下来,我将在/app/Providers
中创建一个服务提供者,如LampServiceProvider.php,并带有绑定:
namespace Apps\Providers;
use Illuminate\Support\ServiceProvider;
class LampServiceProvider extends ServiceProvider {
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(
'App\Repositories\Lamp\LampInterface',
'App\Repositories\Lamp\Lamp'
);
}
}
之后,我会在/app/config/app.php中注册新的服务提供商,最后我可以像这样注入我的接口:
public function switchLampOn(App\Repository\Lamp\LampInterface $lamp)
{
$lamp->on();
}