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

查看:221
本文介绍了如何在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的例子。它使用路线::资源()方法。

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是最好的延伸:
<一href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en\">Chrome扩展邮差REST客户端

这是我简单的路由器和控制器,我也做了同样类型的项目。你可能会想尝试邮差RESTful客户端Chrome浏览器来测试您的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文件

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')));
});

BookmarkController.php
    

BookmarkController.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天全站免登陆