物联网:PHP-Laravel快速部署RESTful(一)

本文介绍了如何利用PHP的Laravel框架快速搭建一个RESTful应用,涉及移植数据库、创建控制器、定义Model和设置ROUTE路由。适用于PHP 5.4+、MySQL 5.1+的环境,讲解了在Laravel中实现MVC架构的关键步骤,特别适合从其他框架转型的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

用下面的代码实现我们称之为Athomes控制器的创建
$ 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路由

在安装目录下打开routes.php
增加
Route::resource('athome', 'AthomesController');  

转载请注明来源于硬件浏览器-UIXO开发框架创始人黄涌(Daniel Hwang

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值