Why in laravel do you to specify classes in composer.json? I thought composer is a program that manages your project's dependencies, yet it's also used to map your controller classes for instance. Why is that?
-
1I think you are using composer / Laravel wrong. Can you post some code and explain further what you're doing? – lukasgeiter Jan 17 '15 at 20:40
-
or is composer and composer.json two different things? – dave Jan 17 '15 at 20:40
-
You don't. There are automatic mappers. – Sergiu Paraschiv Jan 17 '15 at 20:40
-
1`composer.json` is sort of the configuration for composer – lukasgeiter Jan 17 '15 at 20:41
-
for example take this question: http://stackoverflow.com/questions/18850542/laravel-controller-subfolder-routing – dave Jan 17 '15 at 20:41
-
Running `composer dump-autoload` does _not_ alter `composer.json`. – Sergiu Paraschiv Jan 17 '15 at 20:43
-
@dave Do you have any code to show us or is this question based on things you read online? Generally it is possible to autoload single classes with composer but you nearly never have to do that. What do you want to accomplish in the end? – lukasgeiter Jan 17 '15 at 20:45
-
@lukasgeiter - basically i want to do make subfolders in my app/controller folder to organize my controllers better. But from reading the answers to the other question they stated you need to auto load those classes in composer.json first, is that necessary? if not what is the proper way? – dave Jan 17 '15 at 20:48
-
@dave Do you want your controllers to be inside namespaces (depending on the subfolder name) ? – lukasgeiter Jan 17 '15 at 20:50
-
@lukasgeiter now why would i want my controller's in the subfolders be in namespaces? i'm simply just organizing my controller logic better by creating a base controller in each subfolder where all other controllers in those subfolders will extend. Makes sense? – dave Jan 17 '15 at 20:54
-
@dave I was just asking. There are various valid reasons for namespacing controllers. But without namespaces it's even easier. Writing an answer now.... – lukasgeiter Jan 17 '15 at 20:57
-
@lukasgeiter but can you explain to me why put controller classes in namespaces? I need to know – dave Jan 17 '15 at 20:58
-
@lukasgeiter and can you also explain to me why in the other linked question, people suggested putting the controller classes in composer.json and why is composer dump-autoload needed? – dave Jan 17 '15 at 21:00
-
@dave Namespaces, just like directories, can help organize larger applications. One special use case I can imagine is if you have multiple controllers that are actually named the same. This happens often if you have an admin area. For example then you'd have `UserController` and `Admin\UserController`... – lukasgeiter Jan 17 '15 at 21:11
-
@dave Regarding the linked question, Maybe they suggested that because they didn't knew any better. If you look at the top 2 answers, they don't suggest that... The rest should be covered in my answer but feel free to leave a comment if you need clarification – lukasgeiter Jan 17 '15 at 21:12
1 Answers
You don't need to specify your classes in composer.json
. Sure you can do that if you want but for most of the cases there is no need to do so.
Let's take a look at the autoload
section of Laravels default composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
The classmap
basically means autoload everything that's specified here. If it's a directory (like "app/controllers"
) it will load all classes within the folder. recursively.
So just because you want to move your controllers to subdirectories in app/controllers
doesn't mean you have to change anything in composer.json
You have to do one thing though, run composer dump-autoload
. You see, composer creates a file where it stores the classes and the actual file, that contains the class. You can find this file at vendor/composer/autoload_classmap.php
.
The entries look like:
'IndexController' => $baseDir . '/app/controllers/IndexController.php
If you now move IndexController.php
to app/controllers/foo
the application will still try to include it from app/controllers
until you run composer dump-autoload
which will regenerate autoload_classmap.php
.

- 147,337
- 26
- 332
- 270
-
OK so your telling me laravel absolutely depends on composer.json to find where is what in your project? Which includes controllers etc.? And in what cases would you specify controller's in composer.json? And lets say i don't run composer dump-autoload, what would happen? – dave Jan 17 '15 at 21:15
-
You see i come from a codeigniter background, there is no composer.json or similar file/feature in codeigniter, so having a hard time understanding why laravel depends on it... – dave Jan 17 '15 at 21:17
-
And im still trying to understand why composer.json needs to know about where controller classes are? I mean isn't that the whole point of routing to a controller? – dave Jan 17 '15 at 21:18
-
First, yes Laravel depends on composer and uses it for package management and autoloading. – lukasgeiter Jan 17 '15 at 21:25
-
i c...so this is how it works then: first the route receives the uri, then internally laravel checks with its composer.json file to find the controller etc? – dave Jan 17 '15 at 21:27
-
No. Actually Laravel doesn't really communicate with composer. composer is just there to include the files your code lives in. Otherwise all your classes would not be available. Do you understand? – lukasgeiter Jan 17 '15 at 21:29
-
isn't composer just for managing the libraries/dependencies of your project? if so, why the hell is it used for autoloadign classes for instance? – dave Jan 17 '15 at 21:39
-
Yes that's composers purpose. BUT for managing and including dependencies in your application it needs what? A ClassLoader! And composers autoloading just happens to work pretty well so I imagined Taylor Otwell saw no point in creating his own. I really don't understand why this seems to upset you. – lukasgeiter Jan 17 '15 at 21:49
-
i guess coming from codeigniter, its just a little different for me lol...but thanks for the help, much appreciated. – dave Jan 17 '15 at 22:01
-
Using Composer to autoload stuff is way easier than always calling `require_once(path_to_the_class_you_want_to_use.php)`, because all you have to do is use any class without having to deal where it's source file is located. The file location is irrelevant for the code (but not for the developer). Actually, the Laravel classmap autoloading is a BAD example - try to stick to PSR-4 when naming your classes and placing them. – Sven Jan 22 '15 at 21:33