This is related to this question How to register a namespace in laravel 4 but I believe I got that worked out and namespaces are working now.
There is a new problem I've run into. I believe the error is coming from trying to type hint in the controller constructor and has to do with using namespaces and using ioc.
BindingResolutionException: Target [App\Models\Interfaces\PostRepositoryInterface] is not instantiable.
The method below worked perfectly until I tried to introduce namespaces. I can remove all the namespaces and put the interface and repositories in the same directory but would like to know how to make namespaces work with this method of using the ioc.
Here are the relevant files.
routes.php
Route::resource('posts', 'PostsController');
PostController.php
<?php
use App\Models\Interfaces\PostRepositoryInterface;
class PostsController extends BaseController {
public function __construct( PostRepositoryInterface $posts )
{
$this->posts = $posts;
}
}
PostRepositoryInterface.php
<?php namespace App\Models\Interfaces;
interface PostRepositoryInterface {
public function all();
public function find($id);
public function store($data);
}
EloquentPostRepository.php
<?php namespace App\Models\Repositories;
use App\Models\Interfaces\PostRepositoryInterface;
class EloquentPostRepository implements PostRepositoryInterface {
public function all()
{
return Post::all();
//after above edit it works to this point
//error: App\Models\Repositories\Post not found
//because Post is not in this namespace
}
public function find($id)
{
return Post::find($id);
}
public function store($data)
{
return Post::save($data);
}
}
And you can see composer dump-autoload did it's job.
composer/autoload_classmap.php
return array(
'App\\Models\\Interfaces\\PostRepositoryInterface' => $baseDir . '/app/models/interfaces/PostRepositoryInterface.php',
'App\\Models\\Repositories\\EloquentPostRepository' => $baseDir . '/app/models/repositories/EloquentPostRepository.php',
....
)
Any ideas where or what I need to change to make this work with namepaces like it does without them?
Thanks