本文介绍了Laravel 5 REST客户端CRUD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 5中是否有一种使用REST进行CRUD的方法?我已经有一个使用CodeIgniter的REST API,并且希望我的Laravel应用程序与之通信.

Is there a way in Laravel 5 to do a CRUD using REST? I have a REST API already using CodeIgniter and I want my Laravel application to communicate with it.

所以说我有这个网址来获取所有性别: http://api.local/api/api_gender/gender

So let's say I have this url to get all gender: http://api.local/api/api_gender/gender

在我的控制器中,看来我可以做这样的事情:

In my controller, it seems I can do something like this:

$results = json_decode(file_get_contents('http://api.local/api/api_gender/gender/'));

但是我不知道这是否是正确的方法.

But I dont know if this is the right way to do it.

现在,如果我想在Laravel中添加新性别,该怎么办?在CI中,我可以简单地使用Phil的rest库,而只需使用$this->rest->put('http://api.local/api/api_gender/gender/', $parameters)

Now, how could I do it if I want to add a new gender in Laravel? In CI, I could simply use Phil's rest library and just use $this->rest->put('http://api.local/api/api_gender/gender/', $parameters)

推荐答案

我会使用 Guzzle ,这是一个非常流行的,灵活的PHP HTTP客户端.据我所知,它是用PHP发出HTTP请求的最常用的软件包之一.

I would use Guzzle, a very popular, flexible HTTP client for PHP. As far as I know, it is one of the most used packages to make HTTP requests in PHP.

$client = new GuzzleHttp\Client();

$client->put('http://api.local/api/api_gender/gender/', ['json' => ['foo' => 'bar']]);
// or
$client->put('http://api.local/api/api_gender/gender/', ['query' => ['foo' => 'bar']]);

这篇关于Laravel 5 REST客户端CRUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:32