Run Artisan commands form route or controller

Submitted by Bradley - 11 years ago

Laravel 4 have a lot of features some hidden :) for some developers, i found the option to run artisan commands from route or controller, like Artisan::call('migrate:install'); or Artisan::call('migrate', [ '--path'=>'app/database/migrations' ]); Artisan::call('db:seed'); it is usefull when you don't have access to the console in share hosting environments.

//Setup route example
Route::get('/myapp/install/{key?}',  array('as' => 'install', function($key = null)
{
    if($key == "appSetup_key"){
		try {
			echo '<br>init migrate:install...';
			Artisan::call('migrate:install');
			echo 'done migrate:install';
			
			echo '<br>init with Sentry tables migrations...';
			Artisan::call('migrate', [
				'--package'=>'cartalyst/sentry'
				]);
			echo 'done with Sentry';
			echo '<br>init with app tables migrations...';
			Artisan::call('migrate', [
				'--path'     => "app/database/migrations"
				]);
			echo '<br>done with app tables migrations';
			echo '<br>init with Sentry tables seader...';
			Artisan::call('db:seed');
			echo '<br>done with Sentry tables seader';
		} catch (Exception $e) {
			Response::make($e->getMessage(), 500);
		}
	}else{
		App::abort(404);
	}
}
}));