如何在 Laravel 中创建 RESTful API 以在我的 BackboneJS 应用程序中使用 [英] How do I create a RESTful API in Laravel to use in my BackboneJS app

查看:25
本文介绍了如何在 Laravel 中创建 RESTful API 以在我的 BackboneJS 应用程序中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Laravel 4 中创建一个 RESTful API 以在我的 BackboneJS 应用程序中使用.这样做的最佳方法是什么?Laravel 4 框架是否为此提供了很好的解决方案.

I want to create a RESTful API in Laravel 4 to use in my BackboneJS apps. What is the best way for doing this? Does the Laravel 4 framework provides a good solution for this.

推荐答案

这是创建用于存储书签的 API 的示例.它使用 Route::resource() 方法.

This is an example for creating an API that stores bookmarks. It uses the Route::resource() method.

POST = store() (Create a new entry)
DELETE = destroy($id) (Delete an entry)
GET = index() (Get all entries)
GET = show($id) (Get one entry)
PUT = update($id) (Update an entry)

用于测试 API 的最佳扩展:Chrome 扩展 Postman REST 客户端

The best extension for testing your API's: Chrome extension Postman REST client

这是我的简单路由器和控制器,我做了同样的项目.您可能想尝试使用适用于 Chrome 的 Postman RESTful 客户端来测试您的 API,

This is my simple router and controller, I did the same kind of project. You might want to try Postman RESTful client for Chrome to test your API,

routes.php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

// Route group for API versioning
Route::group(array('prefix' => 'api/v1'), function() {
    Route::resource('bookmarks', 'BookmarkController',
        array('except' => array('create', 'edit')));
});

书签控制器.php

class BookmarkController extends Controller {

     /**
        * Display a listing of the resource.
        *
        * @return Response
        */
     public function index() {
            return Bookmark::all();
     }


     /**
        * Store a newly created resource in storage.
        *
        * @return Response
        */
     public function store() {
            $bookmark = new Bookmark;
            $bookmark->url = Input::get('url');
            $bookmark->description = Input::get('description');
            $bookmark->tags = Input::get('tags');
            $bookmark->save();
            return $bookmark;
     }


     /**
        * Display the specified resource.
        *
        * @param  int  $id
        * @return Response
        */
     public function show($id) {
            return Bookmark::find($id);
     }


     /**
        * Update the specified resource in storage.
        *
        * @param  int  $id
        * @return Response
        */
     public function update($id) {
            $bookmark = Bookmark::find($id);
            $bookmark->url = Input::get('url');
            $bookmark->description = Input::get('description');
            $bookmark->tags = Input::get('tags');
            $bookmark->save();
     }


     /**
        * Remove the specified resource from storage.
        *
        * @param  int  $id
        * @return Response
        */
     public function destroy($id) {
            $bookmark = Bookmark::find($id)->delete();
     }

}

这篇关于如何在 Laravel 中创建 RESTful API 以在我的 BackboneJS 应用程序中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆