laravel 4 - Subclassing Migrator not working for namespaced migration -
i have namespaced migrations, , can't past class not found errors due namespacing. in earlier question, antonio carlos ribeiro stated:
laravel migrator doesn't play nice namespaced migrations. best bet in case subclass , substitute migrator class, christopher pitt explains in blog post: https://medium.com/laravel-4/6e75f99cdb0.
i have tried doing (followed composer dump-autoload
, of course), continuing receive class not found errors. i've got project files set as
inetpub |--apptruancy |--database |--2015_04_24_153942_truancy_create_districts.php |--migrationsserviceprovider.php |--migrator.php
the migration file follows:
<?php namespace truancy; use illuminate\database\schema\blueprint; use illuminate\database\migrations\migration; class truancycreatedistricts extends migration { /** * run migrations. * * @return void */ public function up() { schema::create('districts', function($table) { $table->string('id')->unique()->primary()->nullable(false); $table->string('district'); }); } /** * reverse migrations. * * @return void */ public function down() { schema::drop('districts'); } }
migrator.php follows:
namespace truancy; use illuminate\database\migrations\migrator base; class migrator extends base{ /** * resolve migration instance file. * * @param string $file * @return object */ public function resolve($file) { $file = implode("_", array_slice(explode("_", $file), 4)); $class = "truancy\\" . studly_case($file); return new $class; } }
migrationserviceprovider.php follows:
<?php namespace truancy; use illuminate\support\serviceprovider; class truancyserviceprovider extends serviceprovider{ public function register() { $this->app->bindshared( "migrator", function () { return new migrator( $this->app->make("migration.repository"), $this->app->make("db"), $this->app->make("files") ); } ); } }
the lines generated in autoload_classmap.php expected:
'truancy\\migrator' => $basedir . '/apptruancy/database/migrations/migrator.php', 'truancy\\truancycreatedistricts' => $basedir . '/apptruancy/database/migrations/2015_04_24_153942_truancy_create_districts.php', 'truancy\\truancyserviceprovider' => $basedir . '/apptruancy/database/migrations/migrationsserviceprovider.php'
i'm calling php artisan migrate --path="apptruancy/database/migrations"
, receive error:
php fatal error: class 'truancycreatedistricts' not found in c:\inetpub\laravel\vendor\laravel\framework\src\illuminate\database \migrations\migrator.php on line 297
i know must doing dumb (my instinct $class = "truancy\\" . studly_case($file);
in migrator.php wrong), can't unscrew lightbulb. migrate command finding migrations file, , correct classname in classmap, has somewhere in process of resolving classname file, subclass , substitution supposed address. suggestions i've gone wrong?
ok, i've gotten working. turns out medium.com article assumes you'd know put files talks about, didn't. i've made several changes, , working correctly:
- i created new
apptruancy\providers
subfolder, , addcomposer.json
- i moved both
migrator.php
,migrationserviceprovider.php
new folder - i changed namespace in both of files
truancy\providers
match directory structure - i added
'truancy\providers\migrationsserviceprovider'
providers
array inapptruancy\config\app.php
- i added
\
in front ofschema
inmigration
file reference base namespace. - i ran dump-autoload update classmap
this 1 of cases i'm not 100% of changes required, layout make sense i'm happy it. so, in nutshell, if you're trying namespace migrations, need subclass migrator class described in medium.com article listed above, need register service provider in config\app
, making sure class names in both files consistent.
Comments
Post a Comment