Example
Example of Kahire
Kahire can manage API operations with easily. Here is an example for Article API. Lets imagine, we need to design an api for article oprations. And we have Tags, Authors Articles tables.
Create Migrations
First create database migrations for article api.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ArticleTables extends Migration
{
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->timestamps();
$table->increments('id');
$table->string('name');
});
Schema::create('authors', function (Blueprint $table) {
$table->timestamps();
$table->increments('id');
$table->string('name');
});
Schema::create('articles', function (Blueprint $table) {
$table->timestamps();
$table->increments('id');
$table->string('title');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('authors');
});
Schema::create('article_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('article_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
}
public function down()
{
Schema::drop('tags');
Schema::drop('authors');
Schema::drop('articles');
Schema::drop('article_tag');
}
}
Create Models
Lets create Models.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
public $timestamps = true;
protected $table = 'articles';
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function author()
{
return $this->belongsTo(Author::class, 'author_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany(Tag::class, 'article_tag', 'article_id', 'tag_id');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Author extends Model
{
public $timestamps = true;
protected $table = 'authors';
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public $timestamps = true;
protected $table = 'tags';
}
These parts are same as Laravel. Now let's begin design an API part.
Serializers
Serializers are helpers for serialization and deserialization data.
<?php
namespace App\Http\Serializers;
use Kahire\Serializers\Fields\StringField;
use Kahire\Serializers\ModelSerializer;
use App\Article;
class ArticleSerializer extends ModelSerializer
{
protected $model = Article::class;
protected $usePrimaryKey = false;
protected $useTimeStamps = false;
public function generateFields()
{
return [
'title' => StringField::generate(),
'author' => AuthorSerializer::generate(),
'tags' => TagSerializer::generate()->many(),
];
}
}
<?php
namespace App\Http\Serializers;
use Kahire\Serializers\Fields\StringField;
use Kahire\Serializers\ModelSerializer;
use App\Author;
class AuthorSerializer extends ModelSerializer
{
protected $model = Author::class;
protected $useTimeStamps = false;
public function generateFields()
{
return [
'name' => StringField::generate(),
];
}
}
<?php
namespace App\Http\Serializers;
use Kahire\Serializers\Fields\StringField;
use Kahire\Serializers\ModelSerializer;
use App\Tag;
class TagSerializer extends ModelSerializer
{
protected $model = Tag::class;
protected $useTimeStamps = false;
protected $usePrimaryKey = false;
public function generateFields()
{
return [
'name' => StringField::generate(),
];
}
}
So we have serialization objects for manage JSON operations. As you can see, we create fields with generateFields()
method.
Controllers
Kahire can manage all operations for Serializers.
<?php
namespace App\Http\Controllers;
use Kahire\ViewSets\ModelViewSet;
use App\Article;
use App\Http\Serializers\ArticleSerializer;
class ArticleController extends ModelViewSet
{
public $serializer = ArticleSerializer::class;
public $model = Article::class;
}
Routes
And the last part is add controller to Router.
<?php
Route::resource('article', 'ArticleController');
Updated less than a minute ago