Why Laravel migrations class doesn't have a namespace?

1.4k views Asked by At

I am using Laravel 5.1 now.

The migrations class file generated by php artisan make:migration create_users_table --create=users command, would be like this:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    // up and down ...
}

When I edit this migration class file and save it, PHP Code Sniffer in my sublime text will point out a non-standard message of 'Each Class must be in a namespace of at least one level'.

I got no answer in both [https://laravel.com/docs/5.1/migrations] and top batches search results. The related questions on stackoverflow doesn't clear me, either.

Any one knows the reason, or give me some hints? Thanks!

1

There are 1 answers

1
Antonio Carlos Ribeiro On

It's a design matter, basically. There are people out there using namespaced migrations. But the way migrations are loaded and stored in the migration database table, by the migrator, could be a problem to have them namespaced. If you inspect it a little you'll see that the name of the class is part of the name of the migration file and Laravel needs to use that name back to instantiate the migration class to execute it. Having it namespaced could, for example, lead to 2 different classes with the same base class name (class name minus the namespace), which would collide and be very difficult to find. A solution for that is to add the namespace to the migration file name, but they would have to name it something like

2015_12_16_164131_database_migrations|create_users_table.php

Which would translate to a class

Database\Migrations\CreateUsersTable

I'm using | to separate the namespace from the file (class) name, but the whole thing got kind of odd, dont' you think?

Also, as migrations need to be executed in a certain order, the date and timestamp in the beginning of the migration file name is mandatory, so, that's some of the whys.