How to register namespace in laravel 4

Problem: class PostRepostioryInterface not found for line 4 in PostController.php or as for namespace, I even got the class Application \ Models \ Interfaces \ PostRepositoryInterface not found

Questions: How to register namespace in laravel 4? What do I need to do for L4 to recognize classes / interfaces in this namespace?

Larave 3 had a static namespaces object in ClassLoader where you could add namespaces

Autoloader::namespaces(array( 'App\Models\Interfaces' => path('app').'models/interfaces', )); 

I'm not sure I have this right for laravel 3, but in any case, AutoLoader does not exist in Laravel 4 and ClassLoader does exist, but the method namespace does not exist in ClassLoader in Laravel 4.

I looked at this, but it does not work without registering the namespace. Using namespaces in Laravel 4

Structure example:

 app/models/interfaces PostRepostitoryInterface.php app/models/repositories EloquentPostRepository.php namespaces: App\Models\Repositories; App\Models\Interfaces; 

files:

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(); } public function find($id) { return Post::find($id); } public function store($data) { return Post::save($data); } } 

PostController.php

 <?php use App\Models\Interfaces\PostRepositoryInterface; class PostsController extends BaseController { public function __construct( PostRepositoryInterface $posts ) { $this->posts = $posts; } 

thanks

+2
source share
2 answers

On the laravel irc channel, I discovered that namespaces should work in L4 without having to register them anywhere. This is because the dump-autoload linker adds them to the linker / autoload file for me. So this is not a problem.

The problem turned out to be a typo (I can’t find it in the code above, but after passing through each line copying / pasting class names and namespaces something has changed), as well as somehow in my real code, I left the 'use' instruction for EloquentPostRepository .php

 use App\Models\Interfaces\PostRepositoryInterface; 

Now I hit another wall trying to use a name-based interface with ioc and the controller constructor (the target interface of App \ Models \ Interfaces \ PostRepositoryInterface is not real), but I think this should be a different question.

+1
source

You probably forgot to do composer dump-autoload . This updates the list of Laravel autoloads classes.

You can learn more about the documentation for the composer .

+7
source

All Articles