简体   繁体   English

如何在自定义类中注入Laravel 5.2“ Illuminate \\ Http \\ Request”?

[英]How to Inject Laravel 5.2 “Illuminate\Http\Request” in your custom classes?

I am new to laravel and I got a question here. 我是laravel的新手,我在这里有一个问题。 I am trying to inject 'Illuminate\\Http\\Request' to my custom Class. 我试图将“ Illuminate \\ Http \\ Request”注入我的自定义类。

Error: __construct() must be an instance of Illuminate\\Http\\Request, none given,

What is wrong with my code below? 我下面的代码有什么问题? Thanks! 谢谢!

namespace App\Library;

use Crypt;
use Illuminate\Http\Request;

class MenuAccess
{

    public function __construct(Request $request){
        $this->request = $request;
    }

    public function setLoginSession($user){

       $user = Crypt::encrypt($user);

       $this->request->session()->put('PAZPOSID', $user);

       return $request->session()->pull('PAZPOSID');

    }

}

Based on the error given, you are not passing an instance of Illuminate\\Http\\Request to your constructor. 根据给出的错误,您没有将Illuminate\\Http\\Request实例传递给构造函数。

Because you type hinted your parameter, PHP will enforce and make sure it's the same type being passed in. 因为您输入了提示参数,所以PHP将强制执行并确保它与传入的类型相同。

More info: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration 更多信息: http : //php.net/manual/zh/functions.arguments.php#functions.arguments.type-declaration

You can do a dd($request) before you instantiate your class to see its type. 您可以在实例化类以查看其类型之前执行dd($request)

If you want Laravel IoC to auto resolve it for you, have a look here 如果您希望Laravel IoC为您自动解决此问题,请在此处查看

This was an exteremely useful question for me. 对我来说,这是一个极其有用的问题。 And the answer is very useful. 答案非常有用。

Laravel dependency injection seems to work ONLY IN functions and constructors that you don't call by hand (like a constructor of a controller, and a closure (function) that is registered as a route) Laravel依赖注入似乎仅在您不手工调用的函数和构造函数中起作用(例如,控制器的构造函数和注册为路由的闭包(函数))

Try to call by hand the controller constructor which has a dependency injection, and it will NOT WORK. 尝试手动调用具有依赖项注入的控制器构造函数,它将无法正常工作。

Here is an example 这是一个例子

In routes.php 在routes.php中

Route::get('/func_', 'AController@func');

Route::get('/func', function() {
    $c = new App\Http\Controllers\AController;
    return $c->func();
});

Route::get('/f_', 'AController@f');

Route::get('/f', function() {
    $c = new App\Http\Controllers\AController(request());
    return $c->f();
});

Add a controller called AController 添加一个名为AController的控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class AController extends Controller
{
    public $req;
    public function __construct(Request $req) {
        $this->req = $req;
    }

    public function func() {
        return $this->req->input('name');
    }

    public function f(Request $r) {
        return $r->input('name');
    }
}

And try to run /func_ will work because the controller will be instantiated by Laravel /func will NOT work because you tried to call the constructor yourself /f_ will work because the function which use the dependency injection will be called by Laravel /f will NOT work although you created the controller correctly. 并尝试运行/ func_将起作用,因为控制器将由Laravel实例化/ func将不起作用,因为您试图自己调用构造函数/ f_将起作用,因为使用依赖项注入的函数将由Laravel调用/ f将不会尽管您正确创建了控制器,但仍能正常工作。 But the function will not work because called by hand without parameters 但是该函数将不起作用,因为没有参数的情况下手工调用

If you want to call a function which has a dependency injection. 如果要调用具有依赖项注入的函数。 Call with full parameters 使用完整参数调用

I have found my answer. 我找到了答案。 So the problem is in my controller, I need to instantiate in the construct in my controller before I call my custom class. 所以问题出在我的控制器中,我需要在调用自定义类之前实例化控制器中的构造。

And I changed nothing in my Custom Class 我在自定义课程中没有做任何更改

Here is what I did to fix it: 这是我所做的修复工作:

use \App\Library\MenuAccess;

class UserController extends Controller
{
    private $menuAccess;

    public function __construct(MenuAccess $menuAccess)
    {
        $this->menuAccess = $menuAccess;
    }

    public function login(Request $request)
    {
        print_r($this->menuAccess->setLoginSession('user@user.com'));
    }
}

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

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