Laravel 从 REST API 检索数据 [英] Laravel retrieving data from REST API
问题描述
好的,所以我有以下情况:
Okay so I have a following situation:
我正在构建的系统正在从 REST api 检索数据并将该数据保存到数据库中.我想知道的是如何实现这一点,在 Laravel 结构(控制器、模型等)的意义上,这样的行为会发生在哪里?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?
推荐答案
首先你必须在你的 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 调用进行身份验证,您可以删除 'before' =>'auth.basic'
在这里,您可以从 PagesController
访问 index、store、show、update 和 destroy
方法.
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
命令行 CURL 请求将是这样的(这里的用户名和密码是 admin
)并假设您有 .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
接下来,您的 app/controllers
文件夹中有两个名为 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
);
}
}
然后你有一个名为 Page
的模型,它将使用名为 pages
的表.
Then you have model named Page
which will use table named pages
.
<?php
class Page extends Eloquent {
}
您可以使用 Laravel4 Generators 使用 php artisan generator
命令创建这些资源.阅读此处.
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.
这篇关于Laravel 从 REST API 检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!