Library Class not found in Laravel 4 - php

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!

Related

Error Class 'App\\Helpers\\SomeClass' not found in laravel 4 [duplicate]

I have the following error while trying to run my controller
Controller class not found
I have this code in my routes.php file
Route::get('cms/create-page', 'AdminCMSController#create_page');
Route::post('cms/createpage','AdminCMSController#createpage');
Route::controller('cms','AdminCMSController');
And this is the code in my Controller
class AdminCMSController extends BaseController {
public function create_page() {
}
public function createpage() {
}
}
How can I fix it?
If you didn't move the controllers directory from the original location (which is «project_root»/app/controllers/, you must guarantee that:
Laravel's autoload has the controller directory. Navigate to «project_root»/app/start/global.php. You need to have something like this:
(...)
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
(...)
Take notice to this line app_path().'/controllers'. It must exist.
Also, open your composer.json file and verify that the following lines exist:
(...)
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
(...)
Make sure that you have the line with app/controllers
If you have this lines and you still get the same message, go to your project root and run the following command from the command-line composer dumpautoload -o.
Laravel works with Composer, which is a dependency management tool for PHP. It also prepares an autoload file for all your project classes (see composer docs). When you run the composer dumpautoload command, it will create some files within «project_root»/vendor/composer.
Make sure that you can find the class AdminCMSController in the file «project_root»/vendor/composer/autoload_classmap.php. You should see something like this:
'AdminCMSController' => $baseDir . '/app/controllers/AdminCMSController.php',
If you have changed the default location of your controllers directory, you have to do either one of the following steps. However, since you are not defining a namespace in your class, it doesnt seem likely that this is your problem:
Use PSR-0 for autoloading classes. Imagine that you have the following folder structure:
/app
/commands
/config
/database
/Acme
/controllers
You have to specify the Acme folder in your composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Acme": "app/"
}
},
After this you need to update you composer autoload files with the command composer dumpautoload.
If you do not want to use the PSR-0 for autoloading, you need to change your routes file from this
Route::controller('cms','AdminCMSController');
to this:
Route::controller('cms','Acme\controllers\AdminCMSController');
IF you use PSR-0, you need to namespace your classes like this:
<?php namespace Acme\controllers;
class AdminCMSController extends BaseController {
(...)
}
Curious about the Acme reference? I was too. Refer to the wikipedia.

Composer + Laravel 4.2 - Unable to generate PSR-0 autoloader

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.

Laravel 4 - load custom classes

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

Laravel 4 PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found

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.

Custom helper class not loading in Laravel 4

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!

Categories