当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-initializer

A convenient way to initialize your application
授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 钦德佑
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

A convenient way to initialize your application.

Build Status

Introduction

We all know, that every application should contain readme file and Installation section with list of actions that you should to do for preparing an application to work.

Typical instruction:

  • install dependencies
  • run migrations
  • publish assets
  • compiling app assets
  • make cron job for scheduler
  • etc.

Some of actions you should do on every application update (composer update, git pull...) or branch change (git checkout) for preparing an application to work.

Laravel Initializer gives you the ability to declare these processes and run it by simple app:install and app:update artisan commands, which run predefined actions chain depending on the current environment.

Also app:update command could simplify your deploy script in Forge, Envoy.blade.php, laravel-deployer, bash script etc.

With Laravel Initializer you keep both of these processes in the source control.

Put a knowledge of application initialization process into the right place

Installation

Via Composer

composer require mad-web/laravel-initializer

then publish initializer classes:

php artisan vendor:publish --tag=initializers

It will create Install and Update classes in app directorywhich contains local and production methods according to different environments.This methods should return runner chain with specific actions to install or update processes.

You can override config key which stores current environment value, publish config file and set env_config_key value.

php artisan vendor:publish --provider="MadWeb\Initializer\InitializerServiceProvider" --tag=config

by default value is set to app.env for laravel, in most cases you don't need to override this value.

Usage

Usage of app:install and app:update command are the same except that app:install uses Install class and app:update uses Update class.

Install class contents:

namespace App;

use MadWeb\Initializer\Contracts\Runner;

class Install
{
    public function production(Runner $run)
    {
        $run->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
            ->artisan('key:generate')
            ->artisan('migrate', ['--force' => true])
            ->artisan('storage:link')
//            ->dispatch(new MakeCronTask)
            ->external('npm', 'install', '--production')
            ->external('npm', 'run', 'production')
            ->artisan('route:cache')
            ->artisan('config:cache')
            ->artisan('event:cache');
    }

    public function local(Runner $run)
    {
        $run->external('composer', 'install')
            ->artisan('key:generate')
            ->artisan('migrate')
            ->artisan('storage:link')
            ->external('npm', 'install')
            ->external('npm', 'run', 'development');
    }
}

Update class contents:

namespace App;

use MadWeb\Initializer\Contracts\Runner;

class Update
{
    public function production(Runner $run)
    {
        $run->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
            ->external('npm', 'install', '--production')
            ->external('npm', 'run', 'production')
            ->artisan('route:cache')
            ->artisan('config:cache')
            ->artisan('event:cache')
            ->artisan('migrate', ['--force' => true])
            ->artisan('cache:clear')
            ->artisan('queue:restart'); // ->artisan('horizon:terminate');
    }

    public function local(Runner $run)
    {
        $run->external('composer', 'install')
            ->external('npm', 'install')
            ->external('npm', 'run', 'development')
            ->artisan('migrate')
            ->artisan('cache:clear');
    }
}

You can add any other method which should have the same name as your environment name, for example staging, and define different actions.

If you need to run actions with root privileges separately, you can define a method according to the following convention:

namespace App;

use MadWeb\Initializer\Contracts\Runner;
use MadWeb\Initializer\Jobs\Supervisor\MakeQueueSupervisorConfig;
use MadWeb\Initializer\Jobs\Supervisor\MakeSocketSupervisorConfig;

class Install
{
    public function production(Runner $run) { ... }

    public function productionRoot(Runner $run)
    {
        $run->dispatch(new MakeQueueSupervisorConfig)
            ->dispatch(new MakeSocketSupervisorConfig)
            ->external('supervisorctl', 'reread')
            ->external('supervisorctl', 'update');
    }
}

Run it by passing "root" option:

artisan app:install --root

To see details of running actions use verbosity mode:

php artisan app:update -v

You can inject any service from service container in constructor:

class Update
{
    public function __construct(Filesystem $storage)
    {
        $this->storage = $storage;
    }
    // ...
}

If you want to move config classes from the app directory to a different place, rebind app.installer and app.updater keys of service container in the AppServiceProvider.

$this->app->bind('app.installer', \AnotherNamespace\Install::class);
$this->app->bind('app.updater', \AnotherNamespace\Update::class);

Runner API (available actions to run)

$run
    ->artisan('command', ['argument' => 'argument_value', '-param' => 'param_value', '--option' => 'option_value', ...]) // Artisan command
    ->external('command', 'argument', '-param', 'param_value', '--option=option_value', ...) // Any external command by arguments
    ->external('command argument -param param_value --option=option_value') // Any external command by string
    ->callable(function ($arg) {}, $arg) // Callable function (like for call_user_func)
    ->dispatch(new JobClass) // Dispatch job task
    ->dispatchNow(new JobClass) // Dispatch job task without queue
    ->publish(ServiceProvider::class) // Publish single service provider assets
    ->publish([
        ServiceProvider::class,
        AnotherServiceProvider::class,
    ]) // Publish multiple packages assets
    ->publish([ServiceProvider::class => 'public']) // Publish package assets with tag
    ->publish([ServiceProvider::class => ['public', 'assets']]) // Publish package assets with multiple tags
    ->publishForce(ServiceProvider::class) // Force publish, works in any variations
    ->publishTag('public') // Publish specific tag
    ->publishTag(['public', 'assets']) // Publish multiple tags
    ->publishTagForce('public') // Force publish tags

Laravel Nova

If you use Laravel Nova, don't forget to publish Nova assets on each update:

// Update class
$run
    ...
    ->artisan('nova:publish')
    // or
    ->publishTag('nova-assets')

Useful jobs

Laravel initializer provides some useful jobs to make initializing of your application much easier.

Create cron task for scheduling tasks

To enable Laravel Scheduling add dispatch MakeCronTask job to runner chain to create cron task for your application.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\MakeCronTask)

This job will add

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

to crontab list.

Create laravel-echo-server.json config file

If you use Laravel Echo Server for broadcasting events in your application, add dispatch MakeEchoServerConfig job to runner chain to create configuration file.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\MakeEchoServerConfig);

It will create configuration file with default options of laravel-echo-server and prefilled values from your laravel application configuration.

You can override default value by passing array into the job constructor. It would be a good practice to create additional config value for laravel-echo-server in broadcasting.php config:

/*
|--------------------------------------------------------------------------
| Laravel Echo server configurations
|--------------------------------------------------------------------------
|
| Here you may define all of laravel echo server options
|
*/
'server' => [
    'authEndpoint' => '/broadcasting/auth',
    'port' => env('SOCKET_PORT', '6001'),
    'sslCertPath' => env('SSL_CERT', ''),
    'sslKeyPath' => env('SSL_PATH', '')
],

And pass these values to MakeEchoServerConfig job constructor.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\MakeEchoServerConfig(config('broadcasting.server')));

Create supervisor config file for queues

This job creates supervisor config file for queue workers.Add dispatch MakeQueueSupervisorConfig job to runner chain.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\Supervisor\MakeQueueSupervisorConfig);

This job creates configuration file with the command php artisan queue:work --sleep=3 --tries=3 in /etc/supervisor/conf.d/ folder by default, with a filename according to this convention your-application-name-queue.conf.

If you want to override default options, pass it into job constructor.For example if you want to use Laravel Horizon instead of default queue workers.

$run
    ...
    ->dispatch(new \MadWeb\Initializer\Jobs\Supervisor\MakeQueueSupervisorConfig([
        'command' => 'php artisan horizon',
    ]));

Create supervisor config file for laravel echo server

On the same way as MakeQueueSupervisorConfig job, you can use MakeSocketSupervisorConfig to create supervisor config file for launching laravel echo server.The difference from MakeQueueSupervisorConfig is the command node ./node_modules/.bin/laravel-echo-server start and the config filename is your-application-name-socket.conf.

Both config files save log files to your-app-path/storage/logs.

Installation by one command

For running php artisan app:install command, you should install composer dependencies at first.It would be nice to have the ability to install an application by one command. We provide nice hack to implement this behavior.

Add app-install script into scripts section in composer.json.

"scripts": {
    "app-install": [
        "@composer install",
        "@php artisan app:install"
    ],
}

Then you can run just

composer app-install

to initialize your application.

If your application has actions that require root privileges and you use Unix based system, add the following command into your runner chain:

public function production(Runner $run)
{
    $run->artisan(...)
        ...
        ->external('sudo', 'php', 'artisan', 'app:install', '--root');
}

public function productionRoot(Runner $run) { ... }

Safe Update

In cases when latest changes has been pulled from source control and some functionality of currently not installed package is used in one of a Service Provider you will get an error. To prevent this issue you should make composer installat first, to simplify this process you can define app-update script:

"scripts": {
    "app-update": [
        "@composer install",
        "@php artisan app:update"
    ],
},

Then you can run:

composer app-update

Upgrading

Please see UPGRADING for details.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email madweb.dev@gmail.com instead of using the issue tracker.

Credits

Thanks Nuno Maduro for laravel-console-task package which gives pretty tasks outputs

License

The MIT License (MIT). Please see License File for more information.

  • laravel-proxy-manager - 集成 Ocramius/ProxyManager 到 Laravel 应用。 功能 快速创建不同类型的代理实例。 快速绑定不同类型的代理实例到容器。 快速扩展为不同类型的代理实例到容器。 安装 $ composer require guanguans/laravel-proxy-manager -vvv $ php artisan vendor:p

  • 纲要: laravel中redis集群的应用 predis对redis集群模式的底层实现 laravel中redis集群的应用 这部分我想分享下laravel5.2中redis集群的配置(官网也有redis集群的配置讲解,但是5.2版还是有点不足,只是说了将cluster配置项设为true,但光这样一个选项不能代表,一个新手直接可用redis集群,这部分还包括predis客户端的事,所以后面我也会

  • 是否可以访问过滤器内的路由参数? 例如我想访问$ agencyId参数: Route::group(array('prefix' => 'agency'), function() { # Agency Dashboard Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\DashboardC

 相关资料
  • Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr

  • 我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))

  • Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap

  • 简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer

  • 简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果

  • WebStack-Laravel 一个开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己的网址导航。 部署 克隆代码: git clone https://github.com/hui-ho/WebStack-Laravel.git 安装依赖: composer installphp artisan key:generate 编辑配置: cp .env.example .env ...D

  • 百度编辑器 For Laravel 5 支持自定义路由, 默认前后台独立控制器,支持重写方法方便自己的业务逻辑处理,支持扩展图片助手(推荐使用Intervention\Image第三方包) 官网 NinJa911工作室. 疑问讨论 请在issue里new一个. 授权 此Laravel 扩展包基于MIT协议开源MIT license. 安装 1.Composer 安装 composer requir

  • laravel-admin是一个基于laravel的后台管理开发框架,能帮助你使用很少的时间和代码量开发出功能完备的管理后台,另外它作为一个三方包,可以和框架内其它项目并行开发,真正做到前后台开发分离。 功能: RBAC权限管理模块。 菜单管理,和权限系统结合。 模型数据表格,能快速构建数据表格,并支持多种模型关系。 模型表单以及30+种form元素组件,自动实现增、删、改功能。 支持本地和云存储