laravel 控制器不可实例化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26866159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:24:46  来源:igfitidea点击:

laravel controller is not instantiable

phpexceptionlaravelcontrollerinstantiation

提问by Tomas Turan

Im calling my controller in routes.php:

我在routes.php中调用我的控制器:

Route::get('request', 'Controller@request');

In my controller is the method request:

在我的控制器中是方法请求:

class Controller extends BaseController {

   public function request()
   {
        $number1 = int rand ( 10, 20 );
        $number2 = int rand ( 0, 10 );
        return View::make('request', array($number1, $number2));
   }
}

Problem is, that im getting error:

问题是,我收到错误:

exception 'Illuminate\Container\BindingResolutionException' with message 'Target [Controller] is not instantiable.'

I was browsing internet in order to find solution. A lot of simmilar errors are related to interfaces and I couldn't find anything useful. I renamed my controller once, what caused some error, but I repaired it with "composer dump-autoload" command.

我正在浏览互联网以找到解决方案。很多类似的错误都与接口有关,我找不到任何有用的东西。我重命名了我的控制器一次,是什么导致了一些错误,但我用“composer dump-autoload”命令修复了它。

采纳答案by Latheesan

Change the controller class from:

将控制器类从:

class Controller extends BaseController {

To:

到:

class ControllerController extends BaseController {

Why do this? Because Controllerclass already exists in Laravel (BaseControllerextends from Controller), so you probably want to name this to something else.

为什么要这样做?因为Controller类已经存在于 Laravel 中(BaseController从 扩展Controller),所以你可能想把它命名为别的东西。

Also make sure the file is saved as ControllerController.phpin the controllersdir.

还要确保文件保存ControllerController.phpcontrollers目录中。

Also your route should look like this:

您的路线也应如下所示:

Route::get('request', 'ControllerController@request');

I've tested this quickly on my local dev machine and it works.

我已经在我的本地开发机器上快速测试了它并且它有效。

回答by dmisljen

I just had the same problem:

我刚刚遇到了同样的问题:

Target [App\Http\Controllers\Frontend\Booking\BookingController] is not instantiable.

And the problem was that I had declared my constructor as private.

问题是我已经将我的构造函数声明为私有的。

Code for reference:

参考代码:

namespace App\Http\Controllers\Frontend\Booking;

use App\Http\Controllers\Controller;

class BookingController extends Controller
{
    private function __construct() {
         ...
    }
    ...
}

Removing the constructor or making it public resolved my error. Haven't tried actually making it work as private since I'm not that familiar with the whole single instance class (singleton) paradigm or if Laravel supports it (will have to read up on that).

删除构造函数或将其公开解决了我的错误。还没有真正尝试让它作为私有工作,因为我对整个单实例类(单例)范式不太熟悉,或者 Laravel 是否支持它(必须仔细阅读)。

Haven't found this specific situation elsewhere so hope it helps someone.

还没有在其他地方找到这种特定情况,所以希望它可以帮助某人。

回答by itachi

check config/app.phpin aliassection:

config/app.php别名部分:

'Controller'        => 'Illuminate\Routing\Controller',

the controllerclass already exists. if you want it, you have to namespace it.

控制器类已经存在。如果你想要它,你必须命名它。

e.g.

例如

<?php namespace foo;

class controller extends \BaseController {

public function bar(){}

}

in routes:

在路线中:

Route::get('bar',['as'=>'foo.bar', 'uses' => '\foo\controller@bar']);

回答by dafNou

What I did and worked for me is this:

我所做的和对我来说有效的是这样的:

Under app/Http/Controllersuse the classes

Controlles.php

app/Http/Controllers 下使用类

Controlles.php

<?php namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

YourController.php

你的控制器.php

<?php namespace App\Http\Controllers;

class YourController extends Controller
{
    public function request() {
        $number1 = int rand ( 10, 20 );
        $number2 = int rand ( 0, 10 );
        return View::make('request', array($number1, $number2));
    }
}

the call in routes.php:

route.php 中的调用:

Route::get('request', 'YourController@request');