5

I'm using Laravel 5.5 and trying to get used to code by psr-2 standard (just started learning). I analyze all my code with Quafoo QA and go step by step fixing the errors and record them.

By using facades i get this error "Avoid using static access to class". Because of it i'm trying 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 the View Facade i also use DB, Input, Validator and Storage Is this the correct way, are there others?

3
  • 1
    A facade in Laravel provides static interface to methods but those are not really static. So I think you should read about facades in Laravel. Using a Facade is not bad in the context of Laravel (IMO), it's a featured offered by the framework and it'll not hurt.
    – The Alpha
    Mar 6, 2018 at 19:28
  • 1
    Yeah, this is essentially a false positive.
    – ceejayoz
    Mar 6, 2018 at 19:51
  • Indeed Facades are good in Laravel but as i said i'm trying to learn the standard so i needed a workaround that work's everywhere and not just in Laravel. Still thanks for the comments and clarifiactions. @ceejayoz in Laravel it should be a false positive :)
    – Ellesar
    Mar 6, 2018 at 20:36

1 Answer 1

6

You don't need to avoid Facades - they are a key part of the framework. But if you want to, you can use dependency injection to include the classes you need as arguments in the controller methods:

class LoadDataController extends Controller
{

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

  //more code
}

Or if you need that in all the controller methods:

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
}

Of course, this doesn't actually change the functionality of the code you've written, it just rearranges it. I wouldn't take the word of the code analyzer you mentioned as something you are doing wrong. These are standard patterns to use in Laravel.

1
  • 1
    Thanks for the answer: I know about Facades and the fact that in Laravel are not a bad thing. But as i'm trying to learn the standar i want to abstract myself from the framework. Though is kinda of ilogic if i'm using it ^^!
    – Ellesar
    Mar 6, 2018 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.