【问题标题】:How do I create a RESTful API in Laravel to use in my BackboneJS app如何在 Laravel 中创建 RESTful API 以在我的 BackboneJS 应用程序中使用
【发布时间】:2014-06-20 07:40:19
【问题描述】:

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

【问题讨论】:

    标签: api rest backbone.js laravel laravel-4


    【解决方案1】:

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

    在 Laravel 4 中创建一个 RESTful 控制器

    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 extension Postman REST client

    这是我的简单路由器和控制器,我做了同样的项目。您可能想尝试使用适用于 Chrome 的 Postman RESTful 客户端来测试您的 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();
         }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-11
      • 1970-01-01
      • 2023-02-20
      • 2017-01-25
      • 2016-10-05
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多