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/ */
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);
}
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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.