一步步像 cms 一样安装 Laravel 项目

最近公司有个新需求,在前端安装项目的时候,可以像安装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
更新步骤比较简单,执行数据库迁移生成或者修改表。

至此,整个扩展包介绍完了,希望对你有用~~~

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 5年前 自动加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 17

composer require rachidlaasri/laravel-installer这句话开始,这个项目就基本算是失败了,楼主的需求是很多web小白的痛点,很多初级站长都是从WP,dede,帝国这类的产品接触cms的,他们要的是纯界面的安装方式,初级的代码量,composer这个命令就把他们隔离开来,这个方案只能算一个缓解方案,没有解决痛点,我也想解决这个问题,感谢楼主的分享

4年前 评论

@domine 我也想知道要是用户的服务器上没npm,没composer咋弄

4年前 评论

首先开发这个将程序写好,然后安装这个插件 。用户直接下载 上传到服务器,安装即可!

4年前 评论

看完这个教程,决定弄个cms 发布

4年前 评论

我觉得应该做一个安装的控制器来解决。。毕竟使用命令,虚拟机用户咋办。?

4年前 评论
node

666

4年前 评论
hainuo

赞啊,有这个 会让分布虚拟主机用户 用的舒服

4年前 评论

@wongvio 惯例是,vendor,也就是你所说的 依赖项 不会包含在发布的源码包中,是交由下载者自行运行 composer 安装,这是出于减少源码包体积的目的。

当然咯,灵活情况灵活处理。如果这个源码包是面向一般使用者的话,发布源码的时候就可以预先置入依赖项文件(虽然体积会变大),这样的话就可以做到傻瓜安装了(使用者不需要感知 composer)。

@domine 前端处理同理,如果是面向一般使用者的话,完全可以打包好前端文件的 编译产物 (Webpack 构建/ Laravel-Mix 构建),这样在安装的时候就不需要再运行 npm install 了。

4年前 评论

@sunxyw 你的意思是不是这东西给用户的时候依赖都安装好了?

4年前 评论
domine

如何实现执行自定义的command,例如执行npm install 之类的

4年前 评论

@sunxyw 真是这样的话,我得道歉才行了,没仔细看

4年前 评论
sunxyw

@wongvio 我觉得你可能误会了,composer require rachidlaasri/laravel-installer是给程序的开发者用的。开发者配置好该扩展后,就可以将整个项目打包发布,新手只要下载下来,就可以想普通cms一样安装了

4年前 评论

前端安装项目? 前端开发人员安装项目的意思吗?

5年前 评论

这个很赞呀。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!