關于Laravel就不再多說,關于Laravel的內容可以網上搜索,百科上有很多。接下來讓我們用Laravel Resource Controller創建一個RESTful應用吧
開發環境:Windows+XAMPP
代碼下載: http://github.com/gmszone/learingphp
編輯器: Sublime Text3(Crack)
轉載保留: (轉載自Phodal's Blog Phodal's CSDN)
創建數據庫遷移php artisan migrate:make create_athomes_table
打開生成的*create_athome_table.php
添加表
<?phpuse Illuminate/Database/Schema/Blueprint;use Illuminate/Database/Migrations/Migration;html' target='_blank'>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,以及兩個傳感器,還有溫度傳感器
這里要用的是控制器
Route::get('/athome/{atid}',function($atid){ $atdata=Athomes::where('id','=',$atid) ->select('id','temperature','sensors1','sensors2','led1') ->get(); return Response::json($atdata); });創建控制器用下面的代碼實現我們稱之為Athomes控制器的創建
php artisan controller:make AthomesController
就會在app/controllers下面生成下面的代碼
<?phpclass 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) { // }}添加類位于app/modals
<?phpclass Athomes extends Eloquent { protected $table = 'athomes';}添加到路由打開routes.php
Route::resource('athome', 'AthomesController');一個簡單的輸出官方的指導寫明了資源控制器的用法
Verb | Path | Action | Route Name |
---|---|---|---|
echo "Hello,world"
打開瀏覽器,這里是在XAMPP下開關的也就是
localhost/learingphp/public/athome
會看到輸出Hello,World。也就是說athome是直接在Index上的。讓我們輸出JSON
添加seed打開app/database/seeds/DataSeeder.php,創建兩個新的數據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輸出JSON回到index函數,讓我們輸出json格式的數據
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"}]用curl進行測試,現在不是很必要,不過也不影響我們繼續
curl -X GET http://localhost/learingphp/public/athome
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答