これは、この質問に関連しています。laravel4に名前空間を登録する方法ですが、うまくいき、名前空間が機能していると思います。
私が遭遇した新しい問題があります。エラーは、コントローラーコンストラクターにヒントを入力しようとしたことが原因であり、名前空間の使用とiocの使用に関係していると思います。
BindingResolutionException: Target [App\Models\Interfaces\PostRepositoryInterface] is not instantiable.
以下の方法は、名前空間を導入しようとするまでは完全に機能していました。すべての名前空間を削除し、インターフェイスとリポジトリを同じディレクトリに配置できますが、iocを使用するこの方法で名前空間を機能させる方法を知りたいです。
関連するファイルは次のとおりです。
ルート.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);
}
}
そして、あなたは作曲家のダンプを見ることができます-autoloadはそれが仕事をしました。
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',
....
)
これをネームパックなしで機能させるために、どこで、または何を変更する必要があるかについてのアイデアはありますか?
ありがとう