关于Laravel就不再多说,关于Laravel的内容可以网上搜索,百科上有很多。接下来让我们用Laravel Resource Controller创建一个RESTful应用吧
开发环境:Windows+XAMPP
代码下载: https://github.com/gmszone/learingphp
编辑器: Sublime Text3(Crack)
转载保留: (转载自Phodal's Blog Phodal's CSDN)
php artisan migrate:make create_athomes_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'); } }
上一篇博文用的是 Laravel+Angularjs+D3打造可视化数据,RESTful+Ajax 直接路由的方法,这里要用的是控制器
Route::get('/athome/{atid}',function($atid){ $atdata=Athomes::where('id','=',$atid) ->select('id','temperature','sensors1','sensors2','led1') ->get(); return Response::json($atdata); });
php artisan controller:make AthomesController
就会在app/controllers下面生成下面的代码
<?php class AthomesController extends \BaseController { /** * Display a listing of the resource. * * @return Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return Response */ public function create() { // } /** * Store a newly created resource in storage. * * @return Response */ public function store() { // } /** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // } }
<?php class Athomes extends Eloquent { protected $table = 'athomes'; }
Route::resource('athome', 'AthomesController');
Verb | Path | Action | Route Name |
---|---|---|---|
GET | /resource | index | resource.index |
GET | /resource/create | create | resource.create |
POST | /resource | store | resource.store |
GET | /resource/{resource} | show | resource.show |
GET | /resource/{resource}/edit | edit | resource.edit |
PUT/PATCH | /resource/{resource} | update | resource.update |
DELETE | /resource/{resource} | destroy | resource.destroy |
echo "Hello,world"打开浏览器,这里是在XAMPP下开关的也就是
localhost/learingphp/public/athome会看到输出Hello,World。也就是说athome是直接在Index上的。让我们输出JSON
class AthomesTableSeeder extends Seeder { public function run() { Athomes::create(array( 'temperature'=>'19.8', 'sensors1'=>'22.2', 'sensors2'=>'7.5', 'led1'=>False )); Athomes::create(array( 'temperature'=>'18.8', 'sensors1'=>'22.0', 'sensors2'=>'7.6', 'led1'=>False )); } }
php artisan db:seed
public function index() { $data=Athomes::all(); return Response::json($data); }
[{"id":1,"temperature":12,"sensors1":12,"sensors2":23,"led1":0,"created_at":"2013-11-11 08:42:24","updated_at":"2013-11-11 08:42:24"},{"id":2,"temperature":12,"sensors1":12,"sensors2":33,"led1":1,"created_at":"2013-11-11 08:42:33","updated_at":"2013-11-11 08:42:33"},{"id":3,"temperature":19.799999237061,"sensors1":22,"sensors2":7.5,"led1":1,"created_at":"2013-11-11 08:53:41","updated_at":"2013-11-11 08:53:41"},{"id":4,"temperature":19,"sensors1":22,"sensors2":7.5,"led1":1,"created_at":"2013-11-11 08:54:02","updated_at":"2013-11-11 08:54:02"}]