douxingsuo8809 2017-01-19 14:21
浏览 55
已采纳

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

I am injecting a Service in my controller's constructor but unfortunately it does not seem to be working.

This is the controller file:

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');
    }
}

This is my web.php:

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

And that is the stack trace:

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

I have tried several times composer dump-autoload but with no luck. Any ideas are more than welcome. In case you need composer.json could be responsible I can paste it in here.

UPDATE

So, this is my composer.json file:

{
    "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;

and as a file placed under app\Glass\Calculator\Services;

  • 写回答

1条回答 默认 最新

  • doubihuai8468 2017-01-19 14:30
    关注

    Errors like Class Whatever\Class does not exist, when you are using PSR-4, doesn't care about composer update. Composer autoloads them automatically, and usually the problem is one of these:

    1) The class file is misplaced. Check if it is in the proper directory. As example, having this as base namespace for your app:

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

    Means that your files should be

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

    2) The class file is not correctly namespaced. Sometimes we copy files and forget to change the namespace, or we have a typo on them.

    3) The class file has a bug. Sometimes if you have a serious problem in the file, like a missing }, PHP is not able to compile that file and then it basically acts like the file never existed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的