Class autoloading doesn't loads migration classes
Autoloading was recently re-introduced to prevent us from having to run composer dump-autoload every time. See this commit.
But it seems this was only done for controllers and models. When I try to run a migration it still gives a Fatal error saying it didn't find the migration class:
PHP Fatal error: Class 'CreateUsersTable' not found in foo/bar
Running composer dump-autoload fixes the problem.
I've tried adding the migrations folder to the array of folders here in app/start/global:
ClassLoader::register(new ClassLoader(array(
app_path().'/controllers',
app_path().'/models',
app_path().'/database/migrations',
)));
Sadly that doesn't seems to be the solution.
Any suggestions...?
How did you create the migration? Using Artisan? Artisan should dump the autoloads for you when a migration is created.
Also is this on a package or on your app?
It's on the app. I'm using a clean Laravel 4 installation.
I created the migration with Artisan, yes. I did use the --env=local flag because otherwise it would try to use the default database config file instead of my development database config file.
Could it perhaps be a permission problem? Should some folders/files be write able in the /vendor folder?
Where did it place the migration? In app/database/migrations?
Yep, exactly.
I just installed Laravel 4 at home on my home dev machine and here I don't get the error I had at work. I'll try to reinstall again tomorrow at work to see if I can get it to work there as well.
OK. I've never seen this error.
I tried it again by changing some things on the original repository like tweaking permissions and manually removing the Autoload class in vender/composer/autoload_classmap.php and reinstalling the migration but no luck. Here is the problem at hand:
I did: php artisan migrate --env=local
PHP Fatal error: Class 'CreateUsersTable' not found in /foo/bar/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 290
<!DOCTYPE html>
<html>
and so on...
I'm trying to install a fresh Laravel installation but it seems that Github is having some issues at the moment (when i run composer.phar install):

No luck either trying to clone it manually:

I'll try again later.
It was a Github issue apparently: https://status.github.com/messages. I can clone the repo now.
I set up a new Laravel 4 installation, followed the steps I've taken previously and again ran into the same problem.
Here are the exact steps I've taken:
- Followed the Laravel installation guide on http://four.laravel.com/docs/installation
- Set up a virtual host pointed towards the public folder.
- Created a local dev environment in
start.phpwith following code:
$env = $app->detectEnvironment(array(
'local' => array('*.loc'),
));
- Set up a dev database in
app/config/local/database.phpwith following code:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laraveltest',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
- Ran
php artisan migrate:install --env=local - Ran
php artisan migrate:make create_users_table - Set up the database code in the created migration file like this:
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
- Ran
php artisan migrate --env=localand bumping in the error:
PHP Fatal error: Class 'CreateUsersTable' not found in /foo/bar/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 290
<!DOCTYPE html>
<html>
and so on...
That sums up everything. Pretty much stuck now :-/
It's definitely related to my machine because I can get it to work no problems at home, I just have no clue why it doesn't writes the class in the composer Autoload file when it installs the migration.
I've setted permissions to 777 on all files but that doesn't seems to be the problem.
Having the same problem.
I've made a temp workaround by manually adding the classname to the composer autoload file. Add the classname in your migration file to this file: vendor/composer/autoload_classmap.php.
'<class name>' => $baseDir . '/app/database/migrations/<migration filename>.php',
Then run php artisan migrate again (perhaps with the --env= param).
Same problem here as well. Fresh install. On a Mac running PHP 5.4.x.
Running composer dump-autoload fixes the issue... I noticed /vendor/composer/autoload_classmap.php was out of date. So, apparently this command is still needed for some setups. Might be a permissions issue.
Same problem, fixed with composer dump-autoload. Maybe the migrations documentation could be updated to specify that migrate:make does not only create a file, it also updates vendor/composer/autoload_classmap.php.
@taylorotwell:
Artisan should dump the autoloads for you when a migration is created.
OK, but the vendor directory is ignored by git, how to share the updates? Should I add vendor to the project repository, or do you recommend to run composer dump-autoload each time a new migration class has been added by someone else?
I am not familiar with Composer, but it seems awkward to manually handle the class loading system, and to rely on a modified vendor directory :-/
How do you guys have Composer installed on your systems? Using a local composer.phar in the project directory or do you have the composer file in your bin?
In other words, what exact command do you type on the terminal to run Composer?
I've tried it both ways, using a composer.phar file in my bin, and in the local project.
% php artisan migrate
I have composer.phar in my bin and access it via an alias as "composer" - I'm running latest version of Composer.
I have composer.phar installed in /usr/local/bin (working on OS X 10.8).
I run composer with: $ composer.phar install
I run migrations with: $ php artisan migrate --env=local