Laravel请求input()或get() [英] Laravel Request input() or get()

查看:86
本文介绍了Laravel请求input()或get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Laravel 5时,似乎似乎比使用Request门面更喜欢对Request对象进行方法注入.

With Laravel 5 it seems like method injection for the Request object is preferred over using the Request facade.

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index(Request $request)
    {
        $email = $request->input('email');

        // OR

        $email = $request->get('email');
    }
}

我有几个问题:

使用Illuminate\Http\Request比使用Illuminate\Support\Facades\Request

我不知道$ request-> get()是如何解析的,因为Illuminate\Http\Request中没有函数名get(). input()和get()做同样的事情.

I have no idea how $request->get() is resolving as there is no function name get() in Illuminate\Http\Request. input() and get() does the same thing.

方法注入是否比使用Facades更好?

Is method injection better than using Facades?

推荐答案

在控制器方法中,请求注入功能始终是首选,因为在某些方法中它可以帮助您使用表单请求(它们扩展了默认的请求类)验证,即会在输入实际的控制器方法之前自动验证您的请求.这是一个了不起的功能,有助于创建简洁,简洁的控制器代码.

In controller method Request injection functionality is always preferable, because in some methods it could help you to use Form Requests (they are extending default Request class) validation, that will validate your request automatically just before entering to the actual controller method. This is an awesome feature that helps to create slim and clean controller's code.

使用默认的请求注入使控制器的方法相似且易于维护.

Using default Request injection makes your controller's methods similar and easier to maintain.

对象注入总是比Facades更好,因为这种方法对象更容易测试.

Also object injection is always better than Facades, because such methods & objects are easier to test.

get()input()是不同类的方法.第一个是Symfony HttpFoundation Request的方法,input()是Laravel Request类的方法,该方法扩展了Symfony Request类.

get() andinput() are methods of different classes. First one is method of Symfony HttpFoundation Request, input() is a method of the Laravel Request class that is extending Symfony Request class.

这篇关于Laravel请求input()或get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆