laravel 控制器不可实例化

Tom*_*ran 2 php controller exception instantiation laravel

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

Route::get('request', 'Controller@request');
Run Code Online (Sandbox Code Playgroud)

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

class Controller extends BaseController {

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

问题是,我收到错误:

exception 'Illuminate\Container\BindingResolutionException' with message 'Target [Controller] is not instantiable.'
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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

Target [App\Http\Controllers\Frontend\Booking\BookingController] is not instantiable.
Run Code Online (Sandbox Code Playgroud)

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

参考代码:

namespace App\Http\Controllers\Frontend\Booking;

use App\Http\Controllers\Controller;

class BookingController extends Controller
{
    private function __construct() {
         ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

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