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

问题描述

我正在Laravel 5.3中为我的移动应用程序构建后端.我将使用laravel REST API发送/接收和更新数据.

I am building Backend for my Mobile App in Laravel 5.3. I ll be sending/receiving and updating data using laravel REST API.

我希望向使用我的移动应用程序的来宾用户提供很少的数据/信息.几乎没有注册用户的信息/数据.

I want Few data/info to be provided to Guest user who is using my mobile app. and few info/data to registered users.

我想使用REST API

I want to make use of REST API

我尝试了 dingo ,这是很多人推荐的.但是问题是laravel 5.3不支持它,并且它的文档也不是最新的.

I tried dingo which is recommended by many people. But problem is it is not supported with laravel 5.3 and its documentation is also not upto date.

任何人都可以建议我可以用来实现我的目标的任何软件包或代码教程.

Can anyone please suggest any package or code tutorial that i can follow to achieve my goal.

推荐答案

我可以建议 PHP- CRUD-API (我是作者).以下是简短的入门指南.

I can suggest PHP-CRUD-API (I'm the author). Below is the short tutorial to get started.

要添加自动API库,您需要运行:

In order to add the automatic API library you need to run:

php composer.phar require symfony/psr-http-message-bridge
php composer.phar require zendframework/zend-diactoros
php composer.phar require mevdschee/php-crud-api

将"routes/api.php"更改为以下内容,以定义我们的新API路由:

Change the "routes/api.php" to the following content to define our new API route:

<?php

use Psr\Http\Message\ServerRequestInterface;
use Tqdev\PhpCrudApi\Api;
use Tqdev\PhpCrudApi\Config;

Route::any('/{any}', function (ServerRequestInterface $request) {
    $config = new Config([
        'username' => 'php-crud-api',
        'password' => 'php-crud-api',
        'database' => 'php-crud-api',
        'basePath' => '/api',
    ]);
    $api = new Api($config);
    $response = $api->handle($request);
    return $response;
})->where('any', '.*');

在上面的代码中替换字符串"php-crud-api"以匹配您的设置的用户名,密码和数据库(最好从环境变量中读取它们).您应该看到您的应用程序在以下位置运行:

Replace the string "php-crud-api" in the above code to match the username, password and database of your setup (preferably reading them from environment variables). You should see your application running at:

http://127.0.0.1:8000/api/records/posts

用数据库中任何表的名称替换帖子".如果一切正常,那么您应该以JSON格式查看表的内容.

Replace "posts" with the name of any table in your database. If everything works as expected, then you should see the contents of the table in JSON format.

来源: https://tqdev.com/2019-automatic-rest-api -laravel

这篇关于Laravel 5.3 Rest API设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:02