Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How do I create a RESTful API in Laravel to use in my BackboneJS app

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 354
    Comment on it

    We have a situation where we want to create a RESTful API in Laravel 4.x and we have to BackBoneJS in my project .By using below step we can make RESTful API easily using BackboneJS.

    This is an example for creating an API that stores bookmarks. It uses the

    Route::resource()
    

    method.

    Step 1:

    Creating a RESTful controller in Laravel 4

    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)
    

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

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

    By using above code we have implemented all the CRUD function using BackboneJs and Laravel 4.x

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: