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 booksclass 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 interfaceinterface 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

Best Answer


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.

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