如果生产环境中不存在Laravel创建表

I need to use a database to store my sessions with Laravel 4, when the environment is set to production. When it's local the standard file drive system can be used.

I do not have access to the command line, so I'm unable to run any migrations, artisan commands (CloudFoundry setup). So my idea is that in my app/start/global.php file I'd add a check:

if (App::environment('production'))
{
    if(! Schema::hasTable( Config::get('session.table') )) {

        Schema::create( Config::get('session.table'), function($table)
        {
            $table->string('id')->unique();
            $table->text('payload');
            $table->integer('last_activity');
        });

    }
}

If it's currently in production mode (this bit works), then check if there is a table with the name defined in my app/config/session.php file. If not, then create this table with that given name. I noticed the hasTable from the api (http://laravel.com/api/4.1/Illuminate/Database/Schema/Builder.html#method_hasTable).

However, when my app loads, I get the error:

RuntimeException: PDOException was thrown when trying to read the session data: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'table_name.sessions' doesn't exist

Is this because of the lifecycle of the app? How else could I do this...

You can't do this because the session starts before your global.php file is loaded. To be more descriptive, the session gets initiated in the Illuminate\Session\Middleware file within handle method and in the handle method you can see this:

if ($this->sessionConfigured())
{
    $session = $this->startSession($request);

    $request->setSession($session);
}

This happens during the boot process of the framework and after that the $app->booted is called where the global.php file gets included:

// Inside booted handler
$path = $app['path'].'/start/global.php';
if (file_exists($path)) require $path;

So, the session table is required before the Illuminate\Session\Middleware\handle gets called. You should create a table manually and if the environment is production then the table will be used otherwise it won't be used. So what's wrong if you keep a table in your database always ?

Also I'm thinking that, maybe you can create a custom middleware to do this dynamically but not sure whether it's a good idea or not.