1

a quick (maybe stupid) question.

I'm using a namespace for my controllers, like so:

namespace Members;

use DB;
use Input;
use PerformanceReport;
use Redirect;

class AdminController extends MembersController {

And as expected, I have to provide use statements for the Laravel classes I wish to use.

From what I've understood, the composer autoloader prevents this from being necessary if used correctly.

So my question being, is it possible to configure the autoloader to suit my needs, and if so, how would I go by doing this?

5
  • If you use the correct namespace and you use the default Laravel project, you should be fine. Commented Mar 26, 2014 at 20:51
  • Could you elaborate on that? What do you mean by correct namespace and default Laravel project? Commented Mar 26, 2014 at 21:02
  • @TristanGodfrey, your question is not clear, can you please reword it ? Commented Mar 26, 2014 at 21:31
  • @SheikhHeera How can I use Laravel classes in a custom namespace without having to provide use statements for each class? (as demonstrated in my question) Commented Mar 27, 2014 at 8:06
  • with composer you can auto load packages and classes. Please check this video for auto-load classes laracasts.com/lessons/psr-4-autoloading Commented Mar 27, 2014 at 8:39

1 Answer 1

3

Your question is connected with the way PHP namespaces work, not with composer's autoloader.

If your class is in namespace Controllers; and you'd write Redirect::to('/') php would assume that the class you're referring to is in the current declared namespace (in that case Controllers/Redirect). You can either write \Redirect::to('/') or put a use Redirect statement on top like you did.

Composer's autoload just maps namespaces to their file directory (see vendor/composer/autoload_classmap.php for how it maps it).

If you want to dive more into composer's autoloading, i'd recommend read up on PSR-0 and PSR-4.

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

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.