我正在 Laravel 8 中開發(fā)一個(gè)博客應(yīng)用程序。
我正在準(zhǔn)備將其部署在實(shí)時(shí)服務(wù)器上,并且我希望部署過程非常用戶友好。
為此,我一直在為該應(yīng)用程序開發(fā)一個(gè)“安裝程序”:
在 routes\web.php
我有:
Route::get('/install', [InstallController::class, 'index']);
在 app\Http\Controllers\InstallController.php
中,我有這段代碼,以便運(yùn)行遷移如果沒有用戶表:
class InstallController extends Controller { public function index() { if (!Schema::hasTable('users')) { Artisan::call('migrate'); } return redirect('/register')->with('success', 'Way to go! You can create an account.'); } }
上面的代碼有效,所有表都已創(chuàng)建,并且邀請(qǐng)(第一個(gè))用戶注冊(cè)。
問題是我還沒有找到一種方法讓控制器在成功創(chuàng)建表后運(yùn)行數(shù)據(jù)庫播種器。
您可以通過運(yùn)行 php artisan db:seed
或通過 Artisan
外觀來完成此操作,例如 Artisan::call('db:seed');
您的代碼將是:
class InstallController extends Controller { public function index() { if (!Schema::hasTable('users')) { Artisan::call('migrate'); Artisan::call('db:seed'); } return redirect('/register')->with('success', 'Way to go! You can create an account.'); } }
來源: https://laravel.com/docs/9.x/seeding#running-播種機(jī)
但是,我建議不要走這條路,而是創(chuàng)建將為您完成所有這些操作的部署腳本。與此一樣,您將此路由暴露給將使用該應(yīng)用程序的所有用戶,惡意用戶可以利用它。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)