Laravel从REST API获取数据

编程入门 行业动态 更新时间:2024-10-26 06:35:38
本文介绍了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 localhost/project/api/v1/pages // this will call index function POST localhost/project/api/v1/pages // this will call store function GET localhost/project/api/v1/pages/1 // this will call show method with 1 as arg PUT localhost/project/api/v1/pages/1 // this will call update with 1 as arg DELETE 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

接下来,你必须在你的应用程序/控制器文件夹中。

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.

更多推荐

Laravel从REST API获取数据

本文发布于:2023-10-29 13:01:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Laravel   REST   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!