【问题标题】:How to create an API using Laravel 4 [duplicate]如何使用 Laravel 4 创建 API [重复]
【发布时间】:2014-05-21 16:40:30
【问题描述】:

我有一个关于如何使用 laravel PHP 框架创建或构建简单 API 的问题。 我在网上搜索了很多,但没有得到正确的答案。 谁能帮帮我,提前谢谢

【问题讨论】:

标签: php api laravel-4


【解决方案1】:

一种简单的方法是不关心视图。例如:

在您的 UsersController.php 中

public function show($id) {
    $users = User::all();
    return $users;
}

Route.php

Route::get('/api/users', 'UsersController@show');

然后您将获得一个 json 对象作为响应。

然后,您可以使用资源来构建它,以便发布、放置等工作。在 laravel 文档中阅读更多内容:http://laravel.com/docs/routing

Laracasts 有一整套用于构建 API 的系列:https://laracasts.com/series/incremental-api-development

【讨论】:

    猜你喜欢
    • 2013-07-07
    • 2018-02-16
    • 2018-08-14
    • 2014-03-24
    • 2013-06-14
    • 2021-08-16
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多