建站(三)

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
npm 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, 去掉4处 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. Composer Update Fails due to Github Authorization

Composer Update Fails due to Github Authorization

8. 配置信息

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

9. 代码同步

使用homestead虚拟机编写代码并测试,git push,生产环境 git pull,但生产环境的目录 /var/www/project-name 一般是属于www-data用户的,很难使用 git ssh 协议,必须使用 git https 协议即 origin 必须为https://github.com 开头,不能使用 git@github.com 开头,这样就每次 git pull 的时候都要输入github的用户名和密码,很麻烦。网上查到这个 How to enter command with password for git pull?

git config credential.helper 'cache --timeout=3600'

可以在一小时内免去重复输入。

10. 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. composer require guzzlehttp/guzzle:~6.0 Guzzle PHP HTTP Client

  5. composer require "barryvdh/laravel-debugbar:~3.1" --dev

    生成配置文件,存放位置 config/debugbar.php

     php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
    

    打开 config/debugbar.php,将 enabled 的值设置为:

     'enabled' => env('APP_DEBUG', false),
    

    修改完以后, Debugbar 分析器的启动状态将由 .env文件中 APP_DEBUG 值决定。

    参考Easy way to disable debugbar on certain routes / groups / controllers, 可以在控制器内添加如下语句暂时禁用debugbar:

     app('debugbar')->disable();