Laravelコントローラで、すべての関数がRequestを使用する場合は、関数の代わりに直接コンストラクタに要求を挿入するのは正しいですか?それが正しいかどうそして、それは副作用を持っている場合、私はちょうど思っていたLaravel Request:コントローラーコンストラクターにリクエストを直接注入するのは正しいですか?
作品次のコード、...私に簡単に
class BananaController extends Controller
{
protected $request; // request as an attribute of the controllers
public function __construct(Request $request)
{
$this->middleware('auth');
$this->request = $request; // Request becomes available for all the controller functions that call $this->request
}
public function store()
{
$this->validate($this->request, [
'text' => 'required',
]);
// I save the banana attributes and the controller continues...
ゴー、これまで
:-) stackoverflowの上の最初の質問[補遺]明確にするため、「従来型」のコードは次のようになります。
class BananaController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request)
{
$this->validate($request, [
'text' => 'required',
]);
// I save the banana attributes and the controller continues...
マスター/子供のリクエストがあればどうなりますか?どちらがコンストラクタに注入されますか?その目的のために、symfonyはRequest自体の代わりにリクエストスタックサービスを注入することを提案します。 – mmmm
「マスター/子供のリクエスト」とはどういう意味ですか?私はそれをgoogledしかしそれはまだ明確ではない... – TortelliEngineer
小さなアプリでこれは大丈夫です。しかし、今後は大規模なアプリケーションでは、コントローラから検証と認可を分離する必要があります。あなたはたくさんの問題に直面するでしょう – KmasterYC