mercredi 1 juin 2016

Request object type-hinted in a library method throws missing argument error

In Laravel 5.2, it's suggested to type-hint the Request object in controller method declarations:

I'm trying to do the same in one of my libraries:

<?php

namespace App\Libraries;

use Illuminate\Http\Request;

class MyLibrary {

    public static function doStuff(Request $request) {
        //...
    }
}

However, when I try to use the library method like shown in the code below, I get a missing argument error:

<?php

namespace App\Http\Controllers;

use App\Libraries\MyLibrary;

class DefaultController extends Controller {

    public function __construct() {
        MyLibrary::doStuff(); // => trows missing argument error
    }

}

Now, I've type-hinted the Request object into my doStuff() method. Why do I need to pass an argument? I thought that type-hinting is a way to inject needed resources into methods, so they don't have to be always passed directly. Do I understand the concept incorrectly?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire