GithubHelp home page GithubHelp logo

phest's Introduction

phest

###Install with composer

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/ovide/phest.git"
        }
    ],
    "require": {
        "ovide/phest": "dev-master"
    }
}

###Usage example

####index.php

<?php

use Ovide\Libs\Mvc\Rest\App;

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

$loader = new \Phalcon\Loader();
//Register dirs [Optional]
$loader->registerDirs(
		array(
			"./../app/controllers",
			"./../app/models"
		)
)->register();

$app = App::instance();

//Set up the database service
$app->di->set('db', function(){
	return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
			"host" => "localhost",
			"username" => "me",
			"password" => "",
			"dbname" => "MyDB",
			"options" => array(
					PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' //UTF-8 is important for PHP JSON Encoding
			)
	));
});

$app->addResources([MyController::class,otherController::class]);

$app->handle();

####Your controllers

<?php

use Ovide\Libs\Mvc\Rest

class MyController extends Rest\Controller
{
    public function get()
    {
        return User::find()->toArray();
    }

    public function getOne($id)
    {
        return User::findFirst($id)->toArray();
    }

    public function post($obj)
    {
        //Save the object
        $user = new User();
        $user->create($obj);
        return $user->toArray();
    }

    public function put($id, $obj)
    {
        $user = User::findFirst($id);
        //...
        $user->save();
        return $obj;
    }

    public function delete($id)
    {
        $user = User::findFirst($id);
        $user->delete();
        //No return here
    }
}

class Comment extends Rest\Controller
{
    public function get($userID)
    {
        //...
    }

    public function getOne($userID, $commentID)
    {
        //...
    }

    public function post($userID, $obj)
    {
        //...
    }

    public function put($userID, $commentID, $obj)
    {
        //...
    }

    public function delete($userID, $commentID)
    {
        //...
    }
}

Use exceptions if something goes wrong

public function getOne($id)
{
    if (!$foo = Foo::findFirst($id))
        throw new Rest\Exception\NotFound("Ooops! Foo $id not found");
    return $foo->toArray();
}

public function post($fooObj)
{
    if (!$token = getToken()) {
        throw new Rest\Exception\Unauthorized("You must login first");
    }
    if (!canPostWith($token)) {
        throw new Rest\Exception\Forbidden("You can't post here")
    }
    if (alreadyExists($fooObj)) {
        throw new Rest\Exception\Conflict("That object already exists!")
    }
    //...
    return $newObj->toArray();
}

###Next

  • Add header handlers:

    • Content-Type / Accept
    • ETag / If-None-Match
  • Add more options to the router

  • Add HEAD and PATCH verbs

Build Status

phest's People

Contributors

ovide avatar jcheron avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.