一步步像 cms 一樣安裝 Laravel 專案

Kim0801發表於2019-02-22

最近公司有個新需求,在前端安裝專案的時候,可以像安裝CMS一樣,一步步的進行安裝,有這需求的產品不多,百度了挺長時間。。。。功夫不負有心人,終於找到一個工具Laravel Web Installer,竟然還是Laravel的擴充套件包且支援Laravel5.1+版本,簡直是完美定製啊,檢視了下文件,發現配置簡單,功能齊全,下面簡單的介紹下使用方法:

安裝擴充套件包

composer require rachidlaasri/laravel-installer

file
釋出配置檔案:

php artisan vendor:publish --tag=laravelinstaller

可以看到生成了四個檔案和目錄。
第一行,/config/installer.php是此擴充套件包的配置檔案:

<?php

use Illuminate\Validation\Rule;

return [

    /*
    |--------------------------------------------------------------------------
    | Server Requirements
    |--------------------------------------------------------------------------
    |
    | This is the default Laravel server requirements, you can add as many
    | as your application require, we check if the extension is enabled
    | by looping through the array and run "extension_loaded" on it.
    |
    */
    'core' => [
        'minPhpVersion' => '7.0.0'
    ],
    'final' => [
        'key' => true,
        'publish' => false
    ],    
    'requirements' => [
        'php' => [
            'openssl',
            'pdo',
            'mbstring',
            'tokenizer',
            'JSON',
            'cURL',
        ],
        'apache' => [
            'mod_rewrite',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Folders Permissions
    |--------------------------------------------------------------------------
    |
    | This is the default Laravel folders permissions, if your application
    | requires more permissions just add them to the array list bellow.
    |
    */
    'permissions' => [
        'storage/framework/'     => '775',
        'storage/logs/'          => '775',
        'bootstrap/cache/'       => '775'
    ],

    /*
    |--------------------------------------------------------------------------
    | Environment Form Wizard Validation Rules & Messages
    |--------------------------------------------------------------------------
    |
    | This are the default form vield validation rules. Available Rules:
    | https://learnku.com/docs/laravel/5.4/validation#available-validation-rules
    |
    */
    'environment' => [
        'form' => [
            'rules' => [
                'app_name'              => 'required|string|max:50',
                'environment'           => 'required|string|max:50',
                'environment_custom'    => 'required_if:environment,other|max:50',
                'app_debug'             => [
                    'required',
                    Rule::in(['true', 'false']),
                ],
                'app_log_level'         => 'required|string|max:50',
                'app_url'               => 'required|url',
                'database_connection'   => 'required|string|max:50',
                'database_hostname'     => 'required|string|max:50',
                'database_port'         => 'required|numeric',
                'database_name'         => 'required|string|max:50',
                'database_username'     => 'required|string|max:50',
                'database_password'     => 'required|string|max:50',
                'broadcast_driver'      => 'required|string|max:50',
                'cache_driver'          => 'required|string|max:50',
                'session_driver'        => 'required|string|max:50',
                'queue_driver'          => 'required|string|max:50',
                'redis_hostname'        => 'required|string|max:50',
                'redis_password'        => 'required|string|max:50',
                'redis_port'            => 'required|numeric',
                'mail_driver'           => 'required|string|max:50',
                'mail_host'             => 'required|string|max:50',
                'mail_port'             => 'required|string|max:50',
                'mail_username'         => 'required|string|max:50',
                'mail_password'         => 'required|string|max:50',
                'mail_encryption'       => 'required|string|max:50',
                'pusher_app_id'         => 'max:50',
                'pusher_app_key'        => 'max:50',
                'pusher_app_secret'     => 'max:50',
            ],
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Installed Middlware Options
    |--------------------------------------------------------------------------
    | Different available status switch configuration for the
    | canInstall middleware located in `canInstall.php`.
    |
    */
    'installed' => [
        'redirectOptions' => [
            'route' => [
                'name' => 'welcome',
                'data' => [],
            ],
            'abort' => [
                'type' => '404',
            ],
            'dump' => [
                'data' => 'Dumping a not found message.',
            ]
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Selected Installed Middlware Option
    |--------------------------------------------------------------------------
    | The selected option fo what happens when an installer intance has been
    | Default output is to `/resources/views/error/404.blade.php` if none.
    | The available middleware options include:
    | route, abort, dump, 404, default, ''
    |
    */
    'installedAlreadyAction' => '',

    /*
    |--------------------------------------------------------------------------
    | Updater Enabled
    |--------------------------------------------------------------------------
    | Can the application run the '/update' route with the migrations.
    | The default option is set to False if none is present.
    | Boolean value
    |
    */
    'updaterEnabled' => 'true',

];

裡面主要包含所有你部署時對目標伺服器的配置引數、所需php擴充套件、目錄許可權以及.env檔案引數規則定義,這將在安裝時檢視伺服器環境是否符合預期。
第二行,/public/installer是安裝程式的css、js、image等檔案,這裡就不一一展示了,有興趣的自己安裝下看看。
第三行,/resources/views/vendor/installer是程式的模版檔案。
第四行,/resources/lang是程式的語言版本,可自定義設定程式中的文字。

開始安裝

安裝此擴充套件包後,有兩條路由,/install和/update,前者是首次安裝程式路由,後者是以後專案更新時使用的路由。

這裡需要注意的是,第一次安裝預設是/install路由,首次安裝完成後,會在storage資料夾下生成一個installed檔案,表示此專案已經安裝過,如果想再次安裝,需要刪掉這個檔案,否則會出現404。

file
漂亮的安裝介面,當然,你也可以自己修改你想展示的介面及文字。

伺服器環境檢測:
file
許可權檢測:
file
可選的兩種.env檔案配置方法:
file
第一種,表單形式,逐項配置:
file
還有一種是直接複製貼上你的.env檔案資訊:
file
一切配置好後,點選安裝:
file
安裝成功介面:
file
可以看到,這裡展示了資料庫遷移資訊以及.env檔案資訊。點選退出,就可以展示你的專案首頁了,是不是非常方便?

更新專案

由於進入/update路由之前會判斷你專案中datebase/migrations資料夾下的檔案數量,是否大於資料庫migrations表中的記錄行數,如果大於,將呈現更新程式介面,否則,展示404頁面。

更新程式步驟介面:
file
file
file
更新步驟比較簡單,執行資料庫遷移生成或者修改表。

至此,整個擴充套件包介紹完了,希望對你有用~~~

相關文章