简体   繁体   English

我可以从Laravel Controller中删除请求吗

[英]Can I remove Request from Laravel Controller

Can I remove the following line of code 我可以删除以下代码行吗

use Illuminate\Http\Request;

form laravel Controller? 窗体laravel控制器? Is this a good practice? 这是一个好习惯吗? For example, my HomeController: 例如,我的HomeController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $totals = [
            'customers' => \App\Customer::count(),
            'jobs' => \App\Job::count(),
            'invoices' => \App\Invoice::count(),
        ];


        $data = [
            'page_title' => 'Dashboard',
            'totals' => $totals
         ];

        return view('home', $data);
    }

}

Here I don't need the "Request", because none of the functions doesn't use that parameter. 这里我不需要“请求”,因为所有功能都不使用该参数。

To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\\Http\\Request class on your controller method. 要通过依赖注入获得当前HTTP请求的实例,应在控制器方法上键入提示Illuminate\\Http\\Request类。 The incoming request instance will automatically be injected by the service container. 传入的请求实例将由服务容器自动注入。

So if you don't want To obtain an instance of the current HTTP request then remove it. 因此,如果您不想To obtain an instance of the current HTTP request则将其删除。

Yes if you are using just select data query you can go ahead and remove this line. 是的,如果您仅使用选择数据查询,则可以继续并删除此行。 It's need where you will use any get or post form in your class function. 需要在类函数中使用任何get或post形式的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM