My os system: win7 32bit .
Follow the instruction on https://laravel.com/docs/5.2/homestead#per-project-installation.
1. what is homestead?
Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, HHVM, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!
First of all, homestead is a pre-packaged Vagrant box running on a virtual machine like virtual box and VMWare.
Some of us might have experience on using a virtual machine. You can install a linux on your VM so don't have to mess your current OS. That's awesome, but homestead make you even easier because it conclude lots of software you might need on PHP project development. These are below:
Included Software
- Ubuntu 16.04
- Git
- PHP 7.0
- HHVM
- Nginx
- MySQL
- MariaDB
- Sqlite3
- Postgres
- Composer
- Node (With PM2, Bower, Grunt, and Gulp)
- Redis
- Memcached
- Beanstalkd
In short, Homestead is a VM box which include lots of software.
2. Install & Setup
2.1 Install virtualbox 5.x and vagrant.
Easy job, all next and default choice.
2.2 add the
laravel/homesteadbox to your Vagrant installation using the following command in your terminal.
vagrant box add laravel/homestead
so, questions
1. which terminal?
需要安装 Git,想必每个人都是安装了的。这里重点强调一下,后面的所有命令都是在 Git Bash 里面执行,并不是在 Windows 的命令提示符里面执行,这是新手很容易犯的一个错误,切记,一定要在 Git Bash 里面执行命令。
j
2. which directory(folder/mulu)?
Doesn't matter, because whichever directory, doesn't effect the result!
It will take a few minutes to download the box, depending on your Internet connection speed:
Installing Homestead
You may install Homestead by simply cloning the repository. Consider cloning the repository into a
Homestead folder within your "home" directory, as the Homestead box will serve as the host to all of your Laravel projects:
cd ~
进入windows Home 目录
git clone https://github.com/laravel/homestead.git Homestead
安装Homestead通过克隆仓库。“home”目录在windows下就是c:/users/administrator(看你用什么user登录windows)
找到init.bat 文件,运行他
我是在home 目录下Homestead下找到init.bat
要进入Homestead 目录,运行init.bat
在Homestead 目录下有一个 Homestead.yaml 文件,这个就是配置文件了。
ip: "192.168.10.10" // 虚拟机的IP地址,putty连接的地址
xmemory: 2048 // 分配给虚拟机的内存
cpus: 1
provider: virtualbox // 用哪个虚拟机提供者,我选virtual box
authorize: ~/.ssh/id_rsa.pub // 安全证书?
keys:ong- ~/.ssh/id_rsa // 私钥/公钥?
folders: // 共享文件夹,这个很重要,你在宿主机上做的文件改动,都会反映到虚拟机
- map: ~/Code
to: /home/vagrant/Code
sites: // 对应站点,在主机地址栏输入 homestead.app,就去虚拟机 /home/vagrant/Code/public 下找对应的文件,如果没有找到,就会出现 “
No input file specified
”
- map: homestead.app
to: /home/vagrant/Code/public
一定要在主机上编辑,也可以单元测试。虚拟机提供开发环境,但不应该在虚拟机编辑文件。因为在主机上的文件都会同步到虚拟机。
在主机上修改C:\Windows\System32\drivers\etc\hosts
文件,加入
192.168.10.10 homestead.app
启动 vagrant
运行 vagrant up, 有可能要进入 Homestead目录
在主机浏览器地址栏输入 http://homestead.app, 可能会出现
No input file specified
查看site 对应的文件夹 , 比如/home/vagrant/Code/public,有无目录,目录下有无 index.html 或者 index.php 等文件。如果没有就创立一个。问题就能解决。
在Homestead 虚拟机上创建laravel 项目。比如 to-do list。
在虚拟机/home/vagrant/Code/public 输入
composer create-project laravel/laravel quickstart --prefer-dist
会在public目录下产生 quickstart 目录
in the host machine browser , input http://homestead.app/quickstart/public/,
it will display the welcome page of laravel.
Enjoy!!!
到目前为止,我们已经搭好了一个laravel项目的框架。地址栏输入 http://homestead.app/quickstart/public/可以看到laravel的默认首页。下面,我们开始具体讲解,如何进行项目开发。
首先,我们需要创立数据库。和以前开发不同,我们不需要手动创建数据库和表。laravel为我们准备了migration来创立,修改数据库,表等。
在使用migration之前,我们先来简单了解 artisan。artisan是laravel 命令行接口。通过artisan, 我们能在命令行的方式下,运行很多命令,操作数据库,建立模型等等。
php artisan make:migration create_tasks_table --create=tasks
这个方法可以创建一个数据库迁移文件,文件名可能为2017_09_24_213318_create_tasks_table.php
到目前为止,我们已经搭好了一个laravel项目的框架。地址栏输入 http://homestead.app/quickstart/public/可以看到laravel的默认首页。下面,我们开始具体讲解,如何进行项目开发。
首先,我们需要创立数据库。和以前开发不同,我们不需要手动创建数据库和表。laravel为我们准备了migration来创立,修改数据库,表等。
在使用migration之前,我们先来简单了解 artisan。artisan是laravel 命令行接口。通过artisan, 我们能在命令行的方式下,运行很多命令,操作数据库,建立模型等等。
php artisan make:migration create_tasks_table --create=tasks
这个方法可以创建一个数据库迁移文件,文件名可能为2017_09_24_213318_create_tasks_table.php
前面的是时间戳,可能会根据时间不同而改变。
lavarel 为我们搭好了文件的框架,比如列自增ID和时间戳。让我们增加一列name。
将 $table->string('name'); 这句加入到id后面即可。
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tasks');
}
}
保存文件,在命令行执行 php artisan migrate
在虚拟机上进入mysql,
show databases; 列出所有的数据库
use homestead; 使用某个数据库
show tables; 列出这个数据库所有的表
可以看到表 migrations, password_resets,users 这些表是laravel 自建的。tasks表是我们创建的migration文件创建的。
desc tasks;
列出表tasks的结构,可以发现id,name,created_at 和 updated_at 四个字段。name是我们创建的,其他三个字段都是laravel模板创建的。当然,你也可以修改。
自此,表已经建好。下面我们用Eloquent建模。每一个Eloquent模型都对应一个表。
问题, laravel如何知道哪个model对应哪个table?
一个好的命名习惯可以帮助我们解决这个问题。一般来说,表名的命名规范为全部小写,复数形式,比如 table tasks. model命名为Task,即首字母大写,后面小写laravel会自动认为他们为对应的model和table。
我们用这个方法来建立Task model。
php artisan make:model Task
命令运行之后,我们在APP 目录下找到Task.php。打开之后是这样,
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
//
}
这是一个空模型,等待以后填充。
模型建好之后,我们先介绍路由。
No comments:
Post a Comment