Laravel 4 内部请求 (HMVC)

v1.0.2 2014-07-31 03:41 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:29:16 UTC


README

此包不再更新,现在我已经将其拆分为2个。

API是一个用于制作内部请求(HMVC)的工具。

安装

要获取主题的最新版本,只需在您的composer.json文件中要求它。

"teepluss/api": "dev-master"

然后,您需要运行composer install来下载它并更新自动加载器。

一旦安装了主题,您需要将服务提供者注册到应用程序中。打开app/config/app.php并找到providers键。

'providers' => array(

    'Teepluss\Api\ApiServiceProvider'

)

API还附带了一个外观,它提供创建集合的静态语法。您可以在app/config/app.php文件的aliases键中注册外观。

'aliases' => array(

    'API' => 'Teepluss\Api\Facades\API'

)

使用Artisan CLI发布配置。

php artisan config:publish teepluss/api

用法

API帮助您处理内部请求。

内部测试请求。

// GET Request.
API::get('user/1');

// POST Request.
API::post('user', array('title' => 'Demo'));

// PUT Request.
API::put('user/1', array('title' => 'Changed'));

// DELETE Request.
API::delete('user/1');

// Internal request with domain route.
API::invoke('http://api.domain.com', 'post', array('param' => 1))

// You can make remote request without changing code also.
API::post('http://api.github.com', array('username' => 'teepluss'));

// Request remote with invokeRemote.
API::invokeRemote('http://api.github.com', 'post', array('username' => 'teepluss'));

// Get Guzzle to use other features.
$guzzle = API::getRemoteClient();

使用适配器Guzzle进行远程请求。

通过Artisan CLI调用。

// Internal GET.
$ php artisan api:call --request GET /some/route?param=value

// Internal POST.
$ php artisan api:call --request POST /some/form --data "name=Tee"

// Remote request.
$ php artisan api:call --request GET http://google.com

也支持DELETE、PATCH、HEAD

为RESTful创建响应格式。

// Response entries.
$users = User::all();
API::createResponse($users);

// Response entry.
$user = User::find($id);
return API::createResponse($user);

// Response Laravel error.
$errors = $validation->messages()->all(':message');
return API::createResponse(compact('errors'), 400);

// Response created data.
$user = Url::create($data);
return API::createResponse($user, 201);

// Response 404.
API::createResponse("User [$id] was not found.", 404);

//Response deleted.
API::createResponse(null, 204);

对于RESTful响应,建议使用Restable

变更

v1.0.1

  • 添加Artisan CLI。

v1.0.0

  • 发布第一个主版本。

支持或联系

如果您有问题,请联系teepluss@gmail.com

Support via PayPal