I'm new to Laravel, and I'm trying to autoload an entire directory of my own classes. The name of my directory is "templates" so, based on this post in the Laravel forum, I have added my "templates" directory to app/start/global.php like this:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/templates',
));
I then ran composer dump-autoload successfully, but my classes still aren't found. What am I missing?
You may add the directory in composer.json file (in autoload->classmap):
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/controllers/admin",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/templates" // <--------
]
},
Then run (From your project's root folder on terminal/command prompt):
composer dump-autoload
Related
I have directory /libraries/ under /app/. Libraries is suppose to hold all custom files and classes. I'm putted new file there but it doesn't work because I need to dump-autoload.. The problem is that I can't do this. No access to terminal etc..
I have this in composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/libraries", // <<----- this one
"app/tests/TestCase.php"
]
},
Another file which is in libraries from the very beginning is working just fine.
My question is how can I load this new file?
You can try something which I recently tried and it's worked for me. Navigate to vendor/composer/. Since you've said that you have one php file in libraries and it's working this means you will have it in followed files and you can copy/paste the lines and change with your files.
First open file autoload_classmap.php and add at the bottom of the array
'yourfile' => $baseDir . '/app/libraries/yourfile.php',
Second open file autoload_static.php and at the bottom add your file again
public static $classMap = array (
...
'yourfile' => __DIR__. '/../..' . '/app/libraries/yourfile.php',
Save both files and reload. In my case this loaded the files without need of auto-dump.
What i'm trying to do : setup PSR-0 autoloading but composer is generating a PSR-4 autoloader file.
I'm Using:
1) Laravel 4.2
2) Composer version e77435cd0c984e2031d915a6b42648e7b284dd5c 2014-07-02 15:44:54
My composer.json:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries"
],
"files": [
"app/libraries/custom_helpers.php"
],
"psr-0":{
"MyApp":"app/"
}
}
//rest of the file is omitted
After this if i run composer dump-autoload in the terminal it should generate a file in the
vendor/composer directory called autoload_psr0.php
Instead it generates a autolooad_psr4.php and it looks like this :
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
);
From the composer docs i understand that psr-0 is still supported.
Could this be due to to the Monolog package requiring PSR-4 namespacing ?
Well it seems laravel actually doesn't create a separate config for the psr0 standards.
It in fact has a autoload_real.php file by default which references a ClassLoader class and checks for psr-0.
These files can be found in the vendor/composer directory.
I get the following error when I run phpunit with Laravel 4.
PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found in
composer.json
"require": {
"laravel/framework": "4.0.*",
"phpunit/phpunit": "3.7.*"
},
app.php
'Illuminate\Foundation\Testing\TestCase'
What ı should do?
It looks like the autoload doesn't include the new requirement.
Be sure to run composer update to ensure that the file are downloaded and the autoloader is updated with that source.
If the files were downloaded and 'installed' manually run php composer dump-autoload to rebuild the autoload file.
I just ran into the same problem, so I thought I'd post my solution, even though it might be a different solution to what you were after.
I wanted to autoload my own libraries, so I added the following to my composer.json:
"autoload": {
"psr-0": {
"Fhc": "app"
}
},
What I didn't realise was that just above that line was the following:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
},
In essence, my modification had completely overridden the the code above. The solution was to merge the two together (as I should have done to begin with).
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Fhc": "app"
}
},
Now it's all working as expected.
I hope this helps anyone else in the same situation.
Try to remove your "vendor" folder and the file named composer.lock after that run:
composer install
Pay attention to the output produced by composer.
I am currently running into a problem when trying to use a custom helper class in Laravel 4.
I've created a folder in app/libraries which has a custom class MenuComposer.
app/libraries/folder/MenuComposer.php
<?php
namespace 'folder\MenuComposer'
class MenuComposer {
// Code here
}
I've edited composer.json to autoload the app/libraries folder and ran the dump-autoload command in console.
composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries"
]
},
And finally I call the class like so:
View::composer('layouts.back', 'folder/MenuComposer');
Whatever I try, Laravel keeps returning the message Class 'MenuComposer' not found
Does anyone here know what the problem might be?
Your namespace should be declared as the following rather than with quotes:
namespace folder\MenuComposer;
Composer dump-autoload then generates the following in your "/vendor/composer/autoload_classmap":
'folder\\MenuComposer\\MenuComposer' => $baseDir . '/app/libraries/folder/MenuComposer.php'
Which would indicate the class can be reached at:
folder/MenuComposer/MenuComposer
Hope this helps!
I am migrating a working production L3 site to use L4. When a controller calls a library class (app/libraries/adminthing.php), I get the error Error: Class 'adminthing' not found in /var/www/l4/app/controllers/AdminController.php line 15
start/global.php
ClassLoader::addDirectories(array(
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries',
));
I have also done composer dumpautoload after adding the library class. What else did I miss out?
You can autoload folders from composer.json. If you have some custom classes in a folder under /app you can add the folder to composer.json, and after this the classes are auto loaded.
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/customlib" <-- add this
]
},
Then composer dump-autoload, and you can use the classes!