Laravel从REST API获取数据

问题描述:

好了,所以我有以下情况:

Okay so I have a following situation:

我建立该系统是从REST API获取数据和数据保存到数据库中。我想知道是怎么会是这样实施,哪来这样的行为Laravels结构(控制器,模型等)的感觉走?是否Laravel有一个内置的机制,从外部来源检索数据?

The system I am building is retrieving data from a REST api and saving that data into a database. What I am wondering is how could this be implemented and where would behaviour like this go in sense of Laravels structure (controller, model etc.)? Does Laravel have a built in mechanism to retrieve data from external sources?

首先,你必须使你的航线应用程序/ routes.php文件

First you have to make routes in your app/routes.php

/*
    API Routes
*/

Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
    Route::resource('pages', 'PagesController', array('only' => array('index', 'store', 'show', 'update', 'destroy')));
    Route::resource('users', 'UsersController');
});

注意:的如果您不需要身份验证的API调用,您可以删除'之前'=> auth.basic

在这里,您可以访问索引,存储,显示,更新和销毁从方法你的 PagesController

Here you can access index, store, show, update and destroy methods from your PagesController.

和请求的URL会,

GET http://localhost/project/api/v1/pages // this will call index function
POST http://localhost/project/api/v1/pages // this will call store function
GET http://localhost/project/api/v1/pages/1 // this will call show method with 1 as arg
PUT http://localhost/project/api/v1/pages/1 // this will call update with 1 as arg
DELETE http://localhost/project/api/v1/pages/1 // this will call destroy with 1 as arg

命令行卷曲请求将是这样的(这里的用户名和密码管理​​),并假定您有的.htaccess 文件来删除的index.php 从URL,

The command line CURL request will be like this (here the username and password are admin) and assumes that you have .htaccess file to remove index.php from url,

curl --user admin:admin localhost/project/api/v1/pages    
curl --user admin:admin -d 'title=sample&slug=abc' localhost/project/api/v1/pages
curl --user admin:admin localhost/project/api/v1/pages/2
curl -i -X PUT --user admin:admin -d 'title=Updated Title' localhost/project/api/v1/pages/2
curl -i -X DELETE --user admin:admin localhost/project/api/v1/pages/1

接下来,你必须在你的 PagesController.php UsersController.php 两个控制器>应用程序/控制器文件夹中。

Next, you have two controllers named PagesController.php and UsersController.php in your app/controllers folder.

该PagesController.php,

The PagesController.php,

<?php


class PagesController extends BaseController {


    /**
     * Display a listing of the resource.
     *
     * @return Response
     * curl --user admin:admin localhost/project/api/v1/pages
     */

    public function index() {

        $pages = Page::all();;

        return Response::json(array(
            'status' => 'success',
            'pages' => $pages->toArray()),
            200
        );
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     * curl --user admin:admin -d 'title=sample&slug=abc' localhost/project/api/v1/pages
     */

    public function store() {

        // add some validation also
        $input = Input::all();

        $page = new Page;

        if ( $input['title'] ) {
            $page->title =$input['title'];
        }
        if ( $input['slug'] ) {
            $page->slug =$input['slug'];
        }

        $page->save();

        return Response::json(array(
            'error' => false,
            'pages' => $page->toArray()),
            200
        );
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     * curl --user admin:admin localhost/project/api/v1/pages/2
     */

    public function show($id) {

        $page = Page::where('id', $id)
                    ->take(1)
                    ->get();

        return Response::json(array(
            'status' => 'success',
            'pages' => $page->toArray()),
            200
        );
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     * curl -i -X PUT --user admin:admin -d 'title=Updated Title' localhost/project/api/v1/pages/2
     */

    public function update($id) {

        $input = Input::all();

        $page = Page::find($id);

        if ( $input['title'] ) {
            $page->title =$input['title'];
        }
        if ( $input['slug'] ) {
            $page->slug =$input['slug'];
        }

        $page->save();

        return Response::json(array(
            'error' => false,
            'message' => 'Page Updated'),
            200
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     * curl -i -X DELETE --user admin:admin localhost/project/api/v1/pages/1
     */

    public function destroy($id) {
        $page = Page::find($id);

        $page->delete();

        return Response::json(array(
            'error' => false,
            'message' => 'Page Deleted'),
            200
        );
    }

}

那么你就型号命名为将使用指定的表

<?php

class Page extends Eloquent {
}

您可以使用Laravel4发电机创建使用 PHP工匠发电机命令这些资源。阅读这里

You can use Laravel4 Generators to create these resources using php artisan generator command. Read here.

因此​​,使用这条路线分组,你可以使用相同的应用,使API请求和前端。

So using this route grouping you can use the same application to make API request and as a front-end.