0

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;

4
  • Almost sure that you have not registered the custom namespace. Commented Jan 19, 2017 at 14:24
  • Could you elaborate, please? I am sure it will be easy but is this happening in composer.json? Commented Jan 19, 2017 at 14:26
  • Sure. If you have a class file under an unregistered namespace, that is, other than not installed via composer or not under app/ (App namespace) dir, you MUST register namespace in autoload file. Commented Jan 19, 2017 at 14:30
  • Right, I see. Thanks for that, @felipsmartins. I have updated my question, so it might easier for people to understand the issue. Commented Jan 19, 2017 at 14:38

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

If You put it app\ the namespace should be namespace App\Glass\Calculator\Services
Ok sorted! The actual problem was that I need to prefix the namespace with App\ as @Pyton suggests. Question now is how to make Glass\Calculator\Services working (without `App`)
Yeah, so it was number 2) your file was not correctly namespaced.
@thitami You have to create new namespace root in composer.json which will be outside app\ dir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.