laravel 项目 Homestead 开发环境的搭建

做好准备

1
2
cd ~/Homestead && vagrant up
vagrant ssh

在虚拟机中进入 code 文件夹

1
cd ~/code	

Composer 加速

1
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

创建应用

1
composer create-project laravel/laravel yourapp

修改 Hosts

首先找到 Hosts 文件,Windows 下是在

1
C:/Windows/System32/Drivers/etc/hosts

文件成功打开后,在 hosts 文件最后面新增下面一行以完成设置

1
192.168.10.10   yourapp.test

新增站点

进入 homestead 的根目录,找到 Homestead.yaml 并打开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
---
ip: "192.168.10.10"
memory: 2048
cpus: 2
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
- ~/.ssh/id_rsa

folders:
- map: ~/Code
to: /home/vagrant/Code

sites:
- map: homestead.test
to: /home/vagrant/Code/laravel/public
- map: yourapp.test # <--- 这里
to: /home/vagrant/Code/yourapp/public # <--- 这里

databases:
- homestead
- yourapp # <--- 这里

variables:
- key: APP_ENV
value: local

features:
- mysql: true

# blackfire:
# - id: foo
# token: bar
# client-id: foo
# client-token: bar

# ports:
# - send: 93000
# to: 9300
# - send: 7777
# to: 777
# protocol: udp

这里主要设置了 sitesdatabases 两项。 sites 会将域名 larabbs.test 映射到虚拟机的 /home/vagrant/Code/yourapp/public 文件夹上,而 databases 则为新创建的项目指定数据库名

重启虚拟机

Homestead.yaml 文件进行了更改之后,需要运行下面命令来使其更改生效

1
2
3
cd ~/Homestead
vagrant provision
vagrant reload

.env 修改

我们还需要对应用根目录下的 .env 文件进行设置,为应用指定数据库名称 yourapp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
APP_NAME=yourapp
APP_ENV=local
APP_KEY=your_app_key
APP_DEBUG=true
APP_URL=http://yourapp.test
.
.
.
DB_DATABASE=yourapp
DB_USERNAME=homestead
DB_PASSWORD=secret
.
.
.

统一代码风格

对项目的 .editorconfig 进行统一修改。这是为了保证我们的代码风格一致,以避免引起不必要的歧义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2

[*.{js,html,blade.php,css,scss,vue}]
indent_style = space
indent_size = 2

访问应用

在 Chrome 浏览器中打开 yourapp.test 即可

git 版本控制

线上建立好仓库后,记住 ssh 方式连接的代码仓库名,在虚机的项目根目录下输入如下指令即可

1
2
3
4
5
git init
git add -A
git commit -m "初始化项目"
git remote add origin git@e.coding.net:nightingalewk/yanji/yanji.git
git push -u origin master

git 分支的使用

分支使用时,首先执行如下指令

1
2
git checkout master
git checkout -b static-pages

当写完一阶段的代码时,需要合并到主分支,需要如下操作

1
2
3
4
5
git add -A
git commit -m "完成静态页面的建立"
git checkout master
git merge static-pages
git push

启用 Bootstrap

项目中使用 Bootstrap 前端框架,需要先执行以下命令

1
2
composer require laravel/ui --dev
php artisan ui bootstrap

开始安装之前,我们需要设置安装器来使用国内的淘宝镜像加速,加速镜像的原理是使用国内的 CDN 来下载所需代码包,会更加顺畅

1
2
npm config set registry=https://registry.npm.taobao.org
yarn config set registry 'https://registry.npm.taobao.org'

使用用 Yarn 对扩展包进行安装,请在项目根目录下运行以下命令进行安装

1
2
SASS_BINARY_SITE=http://npm.taobao.org/mirrors/node-sass yarn --no-bin-links
yarn add cross-env --no-bin-links

开发常用指令

1
2
//清理各类缓存
php artisan cache:clear && php artisan config:clear && php artisan route:clear && php artisan view:clear