L5:控制器中的服务注入不起作用

时间:2017-01-19 14:21:13

标签: php laravel-5

我正在我的控制器的构造函数中注入一个服务,但不幸的是它似乎没有工作。

这是控制器文件:

namespace App\Http\Controllers;

use Glass\Calculator\Services\CalculatorService;

class CalculatorController extends Controller
{

    /**
     * CalculatorController constructor.
     * @param CalculatorService $calculatorService
     */
    public function __construct(CalculatorService $calculatorService)
    {
        $this->calculatorService = $calculatorService;
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function homepage()
    {
        return view('emoji.homepage');
    }
}

这是我的web.php

Route::get('/','CalculatorController@homepage');

这就是堆栈跟踪:

ReflectionException in Container.php line 809: Class Glass\Calculator\Services\CalculatorService does not exist

    in Container.php line 809
    at ReflectionParameter->getClass() in Container.php line 809
    at Container->getDependencies(array(object(ReflectionParameter)), array()) in Container.php line 790
    at Container->build('App\Http\Controllers\CalculatorController', array()) in Container.php line 644
    at Container->make('App\Http\Controllers\CalculatorController', array()) in Application.php line 709
    at Application->make('App\Http\Controllers\CalculatorController') in Route.php line 203
    at Route->getController() in Route.php line 316
    at Route->controllerMiddleware() in Route.php line 278
    at Route->gatherMiddleware() in Router.php line 666
    at Router->gatherRouteMiddleware(object(Route)) in Router.php line 646
    at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 629
    at Router->dispatchToRoute(object(Request)) in Router.php line 607
    at Router->dispatch(object(Request)) in Kernel.php line 268
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
    at Pipeline->then(object(Closure)) in Kernel.php line 150
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
    at Kernel->handle(object(Request)) in index.php line 54

我曾多次尝试composer dump-autoload,但没有运气。任何想法都非常受欢迎。 如果您需要composer.json负责,我可以将其粘贴到此处。

更新

所以,这是我的composer.json文件:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.3.*"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.0",
        "symfony/css-selector": "3.1.*",
        "symfony/dom-crawler": "3.1.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

CalculatorService:namespace Glass\Calculator\Services;

以及作为app\Glass\Calculator\Services;

下的文件

1 个答案:

答案 0 :(得分:3)

当你使用PSR-4时,像Class Whatever\Class does not exist这样的错误并不关心composer update。 Composer会自动自动加载它们,通常问题就是其中之一:

1)类文件放错了位置。检查它是否在正确的目录中。例如,将此作为应用程序的基本命名空间:

"psr-4": {
    "App\\": "app/",
    "Glass\\": "app/Glass/"
}

表示您的文件应为

/var/www/myapp/app/User.php
/var/www/myapp/app/Glass/Calculator/Services/CalculatorService.php

2)类文件未正确命名空间。有时我们复制文件而忘记更改命名空间,或者我们有错误。

3)类文件有bug。有时,如果文件中存在严重问题,例如缺少},则PHP无法编译该文件,然后它基本上就像文件从未存在过。