PHP code example of origami / api

1. Go to this page and download the library: Download origami/api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

origami / api example snippets



return array(

	'keys' => [
		env('API_KEY', 'secret')
	],

);

Route::group(['prefix' => 'api/v1', 'namespace' => 'Api', 'middleware' => 'Origami\Api\Middleware\AuthenticateApiKey'], function()
{
	Route::get('tasks/{id}', 'Tasks@show');
	Route::post('tasks/{id}', 'Tasks@update');
});

	public function index()
	{
		$items = new Collection(['one','two','three']);

		// Calling with a single argument returns a json response
		return $this->response($items);
	}

	public function index()
	{
		$items = new Collection(['one','two','three']);

		// Calling with no argument returns the response object
		return $this->response()->data($items);
	}

	public function find($id)
	{
		$item = Item::find($id);

		if ( ! $item ) {
			// Using the response object you can call helper methods.
			return $this->response()->errorNotFound();
		}

		return $this->response()->data($item);
	}


	Route::group(['prefix' => 'api/v1', 'namespace' => 'Api', 'middleware' => 'Origami\Api\Middleware\AuthenticateApiKey'], function()
{
    Route::get('items', 'Items@index');
    Route::get('tasks/{id}', 'Tasks@show');
});

 namespace App\Http\Controllers\Api;

use Origami\Api\ApiController;
use App\Items\Item; // Assuming an eloquent Model;
use App\Items\ItemTransformer;

class Items extends ApiController {

	public function index()
	{
		$items = Item::paginate(15);

		return $this->response()->resourcePaginator($items, new ItemTransformer);
	}

	public function show($id)
	{
		$item = Item::find($id);

		if ( ! $item ) {
			return $this->response()->errorNotFound('Item not found');
		}

		return $this->response()->resourceItem($item, new ItemTransformer);
	}

}

 namespace App\Items;

use Origami\Api\Transformer;

class ItemTransformer extends Transformer {

    public function transform(Item $item)
    {
        return [
            'id' => $item->id,
            'title' => $task->title,
            'created_at' => $task->created_at->toDateTimeString()
        ];
    }

}
bash
php artisan vendor:publish