Laravel是PHP的一个快速开发框架。继上一节所讲的,假设你已经了解了一些laravel信息,那么下面我们来讲一下,要让laravel跑通所需要的MVC编程。
MVC 包括Model,View,Controller。要调通laravel这三个要做,外加一个route。
Model即为 MVC 中的 M,翻译为模型,负责跟数据库交互。在 Eloquent 中,数据库中每一张表对应着一个 Model 类。
如果你从其他框架转过来,可能对这里一笔带过的 Model 部分很不适应,没办法,是因为 Eloquent 实在太强大了啦,真的没什么好做的,继承一下 Eloquent 类就能实现很多很多功能了。详见 Eloquent 系列教程:深入理解 Laravel Eloquent(一)——基本概念及用法
View即为 MVC 中的V,翻译为视图,也就是你要看到的网页模样。在app/views文件夹为视图文件夹,视图文件夹可以嵌套。在文件夹里面有叫xxxx.blade.php的文件。
Controller是MVC中的C,翻译为控制器。和View紧密相连。在app/controller文件夹为控制器文件夹。
还要做一个很重要的东西,那就是route。配置路由。这里的路由并不是家里用的无线路由,而是 用户请求的URL到控制器某个方法的转换,function是PHP中代码段的最小单位,所以用户请求的一个路径,如 http://uixo.org/admin/login,这条URL打给路由之后,路由就会去解析,应该调用哪个function,最终返回结果给用户。
接下来让我们用laravel创建一个restful应用吧
条件:软件版本:PHP 5.4+,MySQL 5.1+,linux/CentOS(阿里云)/ubuntu
1.移植数据库
到安装laravel的目录中执行下面命令
$ php artisan migrate:make create_athomes_table
到app/database/migrations/打开生成的*create_athome_table.php
添加表
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAthomesTable extends Migration {
public function up()
{
Schema::create('athomes', function(Blueprint $table)
{
$table--->increments('id');
$table->float('temperature');
$table->float('sensors1');
$table->float('sensors2');
$table->boolean('led1');
$table->timestamps();
});
}
public function down()
{
Schema::drop('athomes');
}
}
这里创建了四个表,一个是用于开关控制的led1,以及两个传感器,还有温度传感器
然后执行迁移:
$ php artisan migrate
2.创建控制器(C)
$ php artisan controller:make AthomesController
就会在app/controllers下面生成AthomesController.PHP,然后修改成
class AthomesController extends \BaseController {
public $restful=true;
protected $athome;
public function __construct(Athomes $athome)
{
$this--->athome = $athome ;
}
public function index()
{
$maxid=Athomes::all();
return Response::json($maxid);
}
public function create()
{
$maxid=Athomes::max('id');
return View::make('athome.create')->with('maxid',$maxid);
}
public function store()
{
$rules = array(
'led1'=>'required',
'sensors1' => 'required|numeric|Min:-50|Max:80',
'sensors2' => 'required|numeric|Min:-50|Max:80',
'temperature' => 'required|numeric|Min:-50|Max:80'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('athome/create')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
$nerd = new Athomes;
$nerd->sensors1 = Input::get('sensors1');
$nerd->sensors2 = Input::get('sensors2');
$nerd->temperature = Input::get('temperature');
$nerd->led1 = Input::get('led1');
$nerd->save();
Session::flash('message', 'Successfully created athome!');
return Redirect::to('athome');
}
}
public function show($id)
{
$myid=Athomes::find($id);
$maxid=Athomes::where('id','=',$id)
->select('id','temperature','sensors1','sensors2','led1')
->get();
return Response::json($maxid);
}
public function edit($id)
{
$athome = Athomes::find($id);
return View::make('athome.edit')
->with('athome', $athome);
}
public function update($id)
{
$rules = array(
'led1'=>'required|',
'sensors1' => 'required|numeric|Min:-50|Max:80',
'sensors2' => 'required|numeric|Min:-50|Max:80',
'temperature' => 'required|numeric|Min:-50|Max:80'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('athome/' . $id . '/edit')
->withErrors($validator);
} else {
$nerd = Athomes::find($id);
$nerd->sensors1 = Input::get('sensors1');
$nerd->sensors2 = Input::get('sensors2');
$nerd->temperature = Input::get('temperature');
$nerd->led1 = Input::get('led1');
$nerd->save();
Session::flash('message', 'Successfully created athome!');
return Redirect::to('athome');
}
}
public function destroy($id)
{
$athome = Athomes::find($id);
$athome->delete();
if(is_null($athome))
{
return Response::json('Todo not found', 404);
}
Session::flash('message', 'Successfully deleted the nerd!');
return Redirect::to('athome');
}
}
3.添加Model(M)
运行:
$ php artisan generate:model athomes
修改文件athomes位于app/models下:
<?php
class Athomes extends Eloquent {
protected $table = 'athomes';
}
4.添加ROUTE路由
Route::resource('athome', 'AthomesController');