wooldrb
wooldrb

Reputation: 33

How To Implement the Driver Pattern Using Helpers in Lumen

How would I go about implementing a driver pattern in Lumen? Right now I have a helper ResponseHandler.php in /app/Helpers which defines an abstract class ResponseHandler.

// app/Helpers/ResponseHandler.php

namespace App\Helpers;

use \Symfony\Component\HttpFoundation\Response as HTTPResponse;

abstract class ResponseHandler extends HTTPResponse
{   

    abstract public function success();
    abstract public function fail();
    [...]
}

I have drivers defined that extend ResponseHandler in the subdirectory /app/Helpers/Response. A driver is defined as follows:

// app/Helpers/Response/JSON.php

namespace App\Helpers\ResponseHandler;

class JSON extends ResponseHandler
{
    public function fail() {
        // logic
    }

    public function success() {
        // logic
    }

    [...]
}

The problem I'm running into is that when I try to use the driver inside a function in my controller, Lumen throws the following error: Class 'App\Helpers\ResponseHandler\JSON' not found. This is the controller I've written (irrelevant parts removed):

// app/Http/Controllers/ResponseController.php

namespace App\Http\Controllers;

use App\Helpers\ResponseHandler\JSON as Response;

class ResponseController extends Controller
{
    public function returnSomething($content) {
        [...]
        return Response::success($_ProcessedContent);
    }

    [...]
}

I've tried changing namespaces around which just ends up causing more errors and doesn't end up solving this one. I suspect that I'm just not familiar enough with namespaces and how Lumen uses them...but I've been working on this problem for a few hours now and can't seem to figure it out.

Could someone with more experience with Lumen/Laravel shed some light onto this issue for me?

* [SOLUTION] * The design pattern was correct, but I needed to run:

composer dump-autoload

after everything was written.

Upvotes: 3

Views: 461

Answers (1)

craig_h
craig_h

Reputation: 32704

Have you tried running: composer dumpautoload from the command line?

Upvotes: 1

Related Questions