| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
WARNING: THIS PLUGIN IS DEPRECATED.
USE https://github.com/cakeplugins/api INSTEAD.
This plugin allows you to easily create flexible restful api's on the fly.
Note: This is a non-stable plugin for CakePHP 3.0 at this time. It is currently under development and should be considered experimental. We are working hard for a first release!
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require cakemanager/cakephp-api:dev-master
Now you need to load the plugin with:
$ bin/cake plugin load -b -r Api
Default the plugin-configurations can be done with Configure::write(). The Api-plugin has an Settings-plugin integration. By adding Configure::write('Api.settings') BEFORE loading the Api-plugin the settings will be saved in your database, and will be configurable in the CakeAdmin Plugin (or your own integration).
The Api-plugin handles the following structure for responses:
{
"url": "/api/blogs/1.json",
"code": 200,
"data": {
"id": 1,
"title": "My first blog",
"body": "How are you?",
"category_id": 1,
"created_by": {
"id": 1,
"email": "bob@cakemanager.org"
},
"modified_by": {
"id": 1,
"email": "bob@cakemanager.com"
},
"created": "2015-06-27T08:39:02+0000",
"modified": "2015-07-03T20:13:09+0000"
}
}
Explanation:
Note: In a later version, you will be able to add your own structure. For now this will be the baseline.
Lets say we want to create an API for our blogs. First of all we need a controller which is prefixed with Api. Use the shell to create this:
$ bin/cake bake controller --prefix Api Blogs
Now the BlogsController is created in src/Controller/Api/BlogsController.php. Remove all crud-actions so we will have an empty controller.
Now add the following to let our api do the work:
use Api\Controller\ApiControllerTrait;
class BlogsController extends AppController
{
use ApiControllerTrait;
public function initialize()
{
parent::initialize();
$this->loadComponent('Api.ApiBuilder', []);
}
}
Explanation:
Now add the following to your config/routes.php:
Router::prefix('api', function ($routes) {
$routes->extensions(['json']);
$routes->resources('Blogs');
});
This will add the BlogsController to the rest-full routes of Cake. For more, read http://book.cakephp.org/3.0/en/development/routing.html#creating-restful-routes.
From now on your api is working, but hold on, no actions are configured yet! Let's add some crud-actions:
$this->loadComponent('Api.ApiBuilder', [
'actions' => [
'index',
'view',
'add',
'edit',
'delete',
]
]);
Now all crud-actions will be added to your api.
The following examples can be used to enable or disable actions:
// enable single action
$this->ApiBuilder->enable('index');
// enable multiple actions
$this->ApiBuilder->enable(['index', 'view']);
// disable single action
$this->ApiBuilder->disable('index');
// disable multiple actions
$this->ApiBuilder->disable(['index', 'view']);
In some cases you have parent resources like:
/api/blogs/:blog_id/comments /api/blogs/:blog_id/comments/:id
Reference: http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes
The Api-plugin has an integration for this parent resources. By calling the index action, the query will automatically validate on the (in this case) blog_id. On save-queries the column will be automatically added. For that, use the following code in your controller:
// in the comments-controller
$this->ApiBuilder->addParentResource('Blogs', 'blog_id');
The first value is the name of the parent resource: Blogs. The second value is the column-name in the table, and also the variable-name in the request (look at the first code-example above). In this case: blog_id.
Of course you will be able customize your own actions. Look at this example:
public function view($id) {
$this->set('data', $this->Blogs->get($id));
return $this->ApiBuilder->execute('view');
}
This example shows you how you will be able to affect the data key of the response and let the ApiBuilder do its work.
Adding your custom actions is easy:
public function active() {
$this->set('data', $this->Blogs->find()->where(['Blogs.active' => true]));
return $this->ApiBuilder->execute();
}
Using this action will return all active blogs on api.org/api/blogs/active.json.
The ApiBuilderComponent has the following configurations:
The bootstrap.php contains the following configurations:
If you need some help or got ideas for this plugin, feel free to chat at Gitter.
Pull Requests are always more than welcome!
| Back | FazBrowse Home | New Git URL |