Avoid the Laravel facade on the controller

I am using Laravel 5.5and trying to get used to the psr-2 standard code (just started learning). I analyze all my code with Quafoo QA and step by step fix bugs and write them down.

Using facades, I get this error "Avoid using static access to class". Because of this, I try to avoid using them.
On my controller, I have this code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Events\FileLoaded;
use Illuminate\Support\Facades\Input;
use Illuminate\Auth\Middleware\Authenticate;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use \Illuminate\Contracts\View\Factory as ViewFactory;

class LoadDataController extends Controller
{

    public function index()
    {
        $viewfactory = app(ViewFactory::class);
        return $viewfactory->make('LoadData/index');
    }

    //more code
}

Besides View Facade, I also use DB, Input, Validator and Storage Is it right, are there others?

+4
source share
1 answer

Facades - . , , , , :

class LoadDataController extends Controller
{

  public function index(ViewFactory $viewFactory)
  {
    return $viewfactory->make('LoadData/index');
  }

  //more code
}

:

class LoadDataController extends Controller
{
  private $viewFactory;

  public function __construct(ViewFactory $viewFactory)
  {
    $this->viewFactory = $viewFactory;
  }

  public function index()
  {
    return $this->viewFactory->make('LoadData/index');
  }

  //more code
}

, , , . , , . Laravel.

+5

Source: https://habr.com/ru/post/1694550/


All Articles