建站(三)

Laravel 学习笔记

Posted by cj on October 10, 2018

Laravel-China 教程 学习笔记

1. 环境搭建

简略步骤

  1. 安装 VirtualBox
  2. 安装 Vagrant
  3. 导入 Homestead Box 虚拟机盒子
  4. 安装 Git
  5. 安装 Homestead 管理脚本
  6. 配置 Homestead.yaml 文件
  7. 启动 Homestead 虚拟机

导入 Homestead Box 虚拟机盒子

下载Homestead 虚拟机盒子, 解压后运行命令

vagrant box add metadata.json

安装 Homestead 管理脚本

使用git-bash运行

cd ~
git clone https://git.coding.net/summerblue/homestead.git Homestead
cd Homestead
git checkout v7.8.0
bash init.sh

2. 解决 Windows 系统使用 Homestead 运行 Laravel 本地项目响应缓慢问题

参考 Speeding up Homestead on Windows Using NFS

a. Install NFS Plugin

vagrant plugin install vagrant-winnfsd

b. Update homestead.rb

There is some weird bug here, not sure why it hasn’t been pushed into the main repo yet, but you will need to replace some code here (if not already live).

homestead/scripts/homestead.rb

Replace the following:

if settings.include? 'folders'
  settings["folders"].each do |folder|
    mount_opts = []

    if (folder["type"] == "nfs")
      mount_opts = folder["mount_opts"] ? folder["mount_opts"] : ['actimeo=1']
    end

    config.vm.synced_folder folder["map"], folder["to"], type: folder["type"] ||= nil, mount_options: mount_opts
  end
end

With this:

if settings.include? 'folders'
  settings["folders"].sort! { |a,b| a["map"].length <=> b["map"].length }

  settings["folders"].each do |folder|
    config.vm.synced_folder folder["map"], folder["to"], 
    id: folder["map"],
    :nfs => true,
    :mount_options => ['nolock,vers=3,udp,noatime']
  end
end

c. Update Homestead.yaml

We can now use nfs in the folders section of the homestead config file.

folders:
    - map: ~\projects
      to: /home/vagrant/Code
      type: nfs

d. 重启homestead

vagrant reload

e. 注意事项

执行vagrant halt后,winnfsd仍然在运行,有时导致rm: cannot remove '<FILENAME>': Input/output error错误,参考https://github.com/winnfsd/vagrant-winnfsd/issues/105,停止虚拟机后手动杀一下winnfsd.exe进程即可。

3. 镜像加速

参考https://laravel-china.org/composer

composer config -g repo.packagist composer https://packagist.laravel-china.org
yarn config set registry https://registry.npm.taobao.org

4. 创建项目

composer create-project laravel/laravel your-project-name --prefer-dist "5.7.*"
composer install
yarn install --no-bin-links

5. 解决yarn install问题

打开pakage.json, 去掉四处cross-env

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    }
}

6. 解决 “/home/vagrant/code/sample/node_modules/pngquant-bin: Command failed.” 问题

参考

错误详情:

error /home/vagrant/code/sample/node_modules/pngquant-bin: Command failed.
Exit code: 1
Command: node lib/install.js
Arguments:
Directory: /home/vagrant/code/sample/node_modules/pngquant-bin
Output:
⚠ The `/home/vagrant/code/sample/node_modules/pngquant-bin/vendor/pngquant` binary doesn't seem to work correctly
  ⚠ pngquant pre-build test failed
  ℹ compiling from source
  ✔ pngquant pre-build test passed successfully
  ✖ Error: pngquant failed to build, make sure that libpng-dev is installed

解决方法:

wget -q -O /tmp/libpng12.deb http://mirrors.kernel.org/ubuntu/pool/main/libp/libpng/libpng12-0_1.2.54-1ubuntu1_amd64.deb   && sudo dpkg -i /tmp/libpng12.deb   && rm /tmp/libpng12.deb
yarn install --no-bin-links

7. 配置信息

  • 修改.envAPP_NAME为项目名称
  • 修改.envAPP_URL为项目URL
  • 修改config/app.phptimezone为上海时区:'timezone' => 'Asia/Shanghai',
  • 修改config/app.phplocale为中文:'locale' => 'zh-CN',

8. composer 组件汇总

  1. composer require dingo/api:2.0.0-alpha2 构建 RestFul Api 的工具包

    首次运行会报错,修改composer.json,添加两行:

     "minimum-stability" : "dev",
     "prefer-stable" : true
    

    保存后再次运行composer require dingo/api:2.0.0-alpha2即可。

    将 dingo 的配置文件 publish 出来到/config/api.php

     php artisan vendor:publish --provider=Dingo\Api\Provider\LaravelServiceProvider
    

    编辑.env:

     API_STANDARDS_TREE=prs
     API_SUBTYPE=larabbs
     API_PREFIX=api
     API_VERSION=v1
     API_DEBUG=true
    
  2. composer require doctrine/dbal 修改数据表字段的属性

  3. composer require liyu/dingo-serializer-switch DingoApi的数据中间件,用来切换 ArraySerializerDataArraySerializer

    安装成功后,在路由文件 Route/Api.php 修改:

     .
     .
     .
     $api->version('v1', [
         'middleware' => 'serializer:array'
     ], function ($api) {
     .
     .
     .
    

    增加中间件 serializer,参数为 array

  4. require guzzlehttp/guzzle:~6.0 Guzzle PHP HTTP Client