1

I have created an interface and so far tested the functions in the model and the interface.

I've now tried to apply it in my controller and initially came up with an error of: Class golfmanager\service\creator\TicketCreatorInterface does not exist

I have carried out a number of composer updates and composer dump-autoload but no change so far.

I added an entry to composer.json to autoload the class as follows:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/helpers",
        "app/golfmanager"
    ],
    "psr-0": {
        "golfmanager": "app/"
    }
},

[updated with corrections]

My controller class which is creating the error looks like this:

use golfmanager\service\creator\TicketCreatorInterface;

 //controller manages the ticket books

class BooksController extends BaseController {

/**
 * Book Repository
 *
 * @var Book
 */
protected $book;
protected $ticket;

public function __construct(Book $book, TicketCreatorInterface $ticket)
{
    $this->book = $book;
    $this->ticket = $ticket;
}
}

My interface is:

namespace golfmanager\service\creator;
//ticket creator interface

interface TicketCreatorInterface {

public function createTicket($input, $book);

 }

Pulling my hair out over this -

Doing a unit test throws up the error, when I try to view the page in the browser I simply get a blank page which never loads

I have a service provider set up as follows:

namespace golfmanager\service;

use Illuminate\Support\ServiceProvider;

class GolfmanagerServiceProvider extends ServiceProvider {

public function register()
{
    $this->app->bind(
        'app\golfmanager\service\creator\TicketCreatorInterface', 
        'app\golfmanager\service\creator\TicketCreator'
        ); 
}

}

I've missed something obvious just can't see it - where have I gone wrong?

[I've updated the methods above to the latest attempts] Update: In config/app.php I have the following under service providers:

//custom service providers
    'golfmanager\service\GolfmanagerServiceProvider'

and my test:

use Mockery as m;
use Way\Tests\Factory;

class BooksTest extends TestCase {

public function __construct()
{
    $this->mock = m::mock('Eloquent', 'Book');
    $this->collection = m::mock('Illuminate\Database\Eloquent\Collection')->shouldDeferMissing();
}

public function setUp()
{
    parent::setUp();

    $this->attributes = Factory::book(['id' => 1]);
    $this->app->instance('Book', $this->mock);
}

public function tearDown()
{
    m::close();
}

public function testIndex()
{
    $this->mock->shouldReceive('all')->once()->andReturn($this->collection);
    $this->call('GET', 'books');

    $this->assertViewHas('books');
}

Current Error:

At the moment the error when I run the test is:

Cannot redeclare class TicketCreatorInterface in C:\wamp\www\golfmanager\golfmanager\app\golfmanager\service\creator\TicketCreatorInterface.php on line 5

When I run through the browser I get:

Class golfmanager\service\creator\TicketCreatorInterface does not exist

very confused - I don't know enought to resolve

Thanks

2 Answers 2

2

You need to include the psr-0 hash inside autoload in your composer.json file.

Like this:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/helpers",
        "app/golfmanager"
    ],
    "psr-0": {
        "golfmanager": "app/"
    }
},

And then composer dump-autoload again.


Edit

I've noticed that you are loading your app/golfmanager both in classmap and psr-0. Remove it from classmap, leaving only the psr-0 entry, like this:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/helpers"
    ],
    "psr-0": {
        "golfmanager": "app/"
    }
},

Now, composer dump-autoload as usual, and you should get rid of the duplicated error.

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

10 Comments

Thank you. I've changed that but now get an error: `Cannot redeclare class TicketCreatorInterface in C:\wamp\www\golfmanager\golfmanager\app\golfmanager\service\creator\TicketCreatorInterface.php on line 5' I've searched through my files and can't see anywhere else (that I understand) where this is being initiated. Confused!
I've tweaked with removing app/golfmanager from the autoload class map but then I get a fail on loading serviceprovider. When I run a unit test I get a cannot redeclare class TicketInterface' but then when I run the app in the browser I get an error: Class golfmanager\service\creator\TicketCreatorInterface does not exist.
tried removing the use golfmanager\service\creator\TicketCreatorInterface from books controller but then an error: Target [TicketCreatorInterface] is not instantiable.
tried adding namespace to serviceprovider, TicketCreatorInterface and TicketCreator which then creates a service provider not found error. Running around in circles here (wrestling jelly springs to mind!) (did a composer dump autoload on each of these
Ok-changed as to your edit composer dump-autoload - the error now is: 'Target [golfmanager\service\creator\TicketCreatorInterface] is not instantiable. So progress! Appreciate the help
|
0

In addition to Manuel's answers and excellent help the final element that was wrong was in the namespace.

The interfaces existed in golfmanager\service\creator but I was trying to reference them in app\golfmanager\service\creator

so the binding is now:

class GolfmanagerServiceProvider extends ServiceProvider {

public function register()
{
    $this->app->bind(
        'golfmanager\service\creator\TicketCreatorInterface', 
        'golfmanager\service\creator\TicketCreator'
        ); 
}

}

Comments

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.