所以我创建了自己的博客包,其结构为Packages/Sitemanager/Blog
我有一个如下所示的服务提供商:
namespace Sitemanager\Blog;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
class BlogServiceProvider extends LaravelServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot() {
$this->handleConfigs();
$this->handleMigrations();
$this->handleViews();
$this->handleRoutes();
}
/**
* Register the service provider.
*
* @return void
*/
public function register() {
// Bind any implementations.
$this->app->make('Sitemanager\Blog\Controllers\BlogController');
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return [];
}
private function handleConfigs() {
$configPath = __DIR__ . '/config/blog.php';
$this->publishes([$configPath => config_path('blog.php')]);
$this->mergeConfigFrom($configPath, 'blog');
}
private function handleTranslations() {
$this->loadTranslationsFrom(__DIR__.'/lang', 'blog');
}
private function handleViews() {
$this->loadViewsFrom(__DIR__.'/views', 'blog');
$this->publishes([__DIR__.'/views' => base_path('resources/views/vendor/blog')]);
}
private function handleMigrations() {
$this->publishes([__DIR__ . '/migrations' => base_path('database/migrations')]);
}
private function handleRoutes() {
include __DIR__.'/routes.php';
}
}
现在,我想做的是动态运行迁移,如果它们之前或在安装过程中从未运行过(我想)。我在旧文档中看到你可以这样做:
Artisan::call('migrate', array('--path' => 'app/migrations'));
但是,这在 laravel 5 中无效,我该如何解决这个问题?
Artisan::call('migrate', array('--path' => 'app/migrations'));
将在 Laravel 5 中工作,但您可能需要进行一些调整。
首先,你需要一个use Artisan;
文件顶部的行(其中use Illuminate\Support\ServiceProvider...
是),因为 Laravel 5 的命名空间。 (你也可以这样做\Artisan::call
- the \
很重要)。
您可能还需要执行以下操作:
Artisan::call('migrate', array('--path' => 'app/migrations', '--force' => true));
The --force
是必要的,因为 Laravel 默认情况下会在生产中提示您是/否,因为它是一个潜在的破坏性命令。没有--force
,你的代码将只是坐在那里旋转(Laravel 正在等待 CLI 的响应,但你没有inCLI)。
我鼓励你在某个地方做这些事情other比boot
服务提供商的方法。这些可能是繁重的调用(依赖于您不想在每个页面浏览量上进行的文件系统和数据库调用)。请考虑使用显式安装控制台命令或路径。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)