100hz/hive

A simple framework for generating RESTful APIs with PHP and MongoDB.

dev-master 2016-12-12 10:01 UTC

README

Build Status

A simple framework for generating RESTful APIs with PHP and MongoDB. For a quickstart please check hive-app!

Goal

This project was started as proof of concept, that one can create RESTful APIs with PHP and MongoDB as simple as with node.js/express.js or similar frameworks/technologies. Also check the other modules belonging to the hive project:

  1. hive-app: The Hive App base distribution, coming with a real life example
  2. hive-console: The Hive console, coming with useful commands for rapid application development
  3. hive-ui: The Hive UI, consumed by hive-app for offering a backend UI
  4. hive-helper: The Hive Helper with some common helper methods

Getting started

  1. Install MongoDB, e.g.: brew install mongodb, see Installation
  2. Install mongodb driver for PHP: pecl install mongodb, see Installation
  3. Require Hive in your project with composer require 100hz/hive --ignore-platform-reqs (Doctrine ODM is not ready for the mongodb driver, therefore the --ignore-platform-reqs flag, please see mongo-php-adapter)

Usage

The model

Create a doctrine annotated MongoDB model, like:

<?php

namespace Hive\Demo\Model;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document
 */
class User
{
    /**
     * @ODM\Id
     *
     */
    public $id;

    /**
     * @ODM\Field(type="string")
     */
    public $email;

    /**
     * @ODM\Field(type="string")
     */
    public $name;

    /**
     * @ODM\Field(type="string")
     */
    public $url;
}

Note: Fields are public is convention, KISS

The api

Create an index.php in your web root with:

<?php

require_once __DIR__ . '/../vendor/autoload.php';


use Hive\Hive;

Hive::create(['security' => false])
    ->resource('Hive\Demo\Model\User')
    ->hive()
    ->api();

That's it. Try e.g. with Postman to request http://localhost/users per POST, Content-Type=application/json and a meaningful JSON body

Contributors

Want to contribute?

Welcome! Glad, to hear. It's easy. Just follow theses steps:

  1. Fork the project & branch
  2. Code and document your stuff
  3. Create a Pull Request with description

License

(MIT License)

Copyright (c) 2016 Sebastian Krüger sk@theblackestbox.net

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.