1. Go to this page and download the library: Download rjapi/raml-json-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/ */
rjapi / raml-json-api example snippets
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use SoliDry\Exceptions\ErrorHandler;
class Handler extends ExceptionHandler
{
use ErrorHandler;
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return JsonResponse
*/
public function render($request, Exception $exception): JsonResponse
{
return $this->renderJsonApi($request, $exception); // method you should call to return json-api response
}
}
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
// ...
protected function mapApiRoutes()
{
// Route::prefix('api') // you don't need prefixes then
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
Modules/{ModuleName}/Http/Controllers/ - contains Controllers that extends the DefaultController (descendant of Laravel's Controller)
Modules/{ModuleName}/Http/FormRequest/ - contains forms that extends the BaseFormRequest (descendant of Laravel's FormRequest) and validates input attributes (that were previously defined as *Attributes)
Modules/{ModuleName}/Entities/ - contains mappers that extends the BaseModel (descendant of Laravel's Model) and maps attributes to RDBMS
Modules/{ModuleName}/Routes/api.php - contains routings pointing to Controllers with JSON API protocol support
Modules/{ModuleName}/Database/Migrations/ - contains migrations created with option --migrations
namespace Modules\V1\Http\Controllers;
class ArticleController extends DefaultController
{
}
namespace Modules\V1\Http\Controllers;
use SoliDry\Extension\BaseController;
class DefaultController extends BaseController
{
}
namespace Modules\V2\Http\Requests;
use SoliDry\Extension\BaseFormRequest;
class ArticleFormRequest extends BaseFormRequest
{
// >>>props>>>
public $id = null;
// Attributes
public $title = null;
public $description = null;
public $url = null;
public $show_in_top = null;
public $status = null;
public $topic_id = null;
public $rate = null;
public $date_posted = null;
public $time_to_live = null;
public $deleted_at = null;
// <<<props<<<
// >>>methods>>>
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'title' => ' ];
}
// <<<methods<<<
}
namespace Modules\V2\Entities;
use Illuminate\Database\Eloquent\SoftDeletes;
use SoliDry\Extension\BaseModel;
class Article extends BaseModel
{
use SoftDeletes;
// >>>props>>>
protected $dates = ['deleted_at'];
protected $primaryKey = 'id';
protected $table = 'article';
public $timestamps = false;
public $incrementing = false;
// <<<props<<<
// >>>methods>>>
public function tag()
{
return $this->belongsToMany(Tag::class, 'tag_article');
}
public function topic()
{
return $this->belongsTo(Topic::class);
}
// <<<methods<<<
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticleTable extends Migration
{
public function up()
{
Schema::create('article', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 256);
$table->index('title', 'idx_title');
$table->string('description', 1024);
$table->string('url', 255);
$table->unique('url', 'idx_url');
// Show at the top of main page
$table->unsignedTinyInteger('show_in_top');
$table->enum('status', ["draft","published","postponed","archived"]);
// ManyToOne Topic relationship
$table->unsignedInteger('topic_id');
$table->foreign('topic_id', 'idx_fk_topic_id')->references('id')->on('topic')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('article');
}
}
http://example.com/api/v1/article?
http://example.com/api/v1/article/1?
http://example.com/api/v1/article?
http://example.com/api/v1/article?
return [
'modules'=> [
'v1',
]
];
return [
'name'=>'V1',
'query_params'=> [
// default settings
'limit' => 15,
'sort' => 'desc',
// access token to check via global FormRequest
'access_token' => 'db7329d5a3f381875ea6ce7e28fe1ea536d0acaf',
],
];
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Modules\V2\Http\Requests\ApiAccessToken::class,
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'jwt' => \SoliDry\Extension\BaseJwt::class,
namespace Modules\V2\Entities;
use Illuminate\Database\Eloquent\SoftDeletes;
use SoliDry\Extension\BaseModel;
class Article extends BaseModel
{
use SoftDeletes;
// >>>props>>>
protected $dates = ['deleted_at'];
// ...
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticleTable extends Migration
{
public function up()
{
Schema::create('article', function(Blueprint $table) {
// ...
$table->softDeletes();
// ...
});
}
// ...
}
namespace Modules\V1\Http\Controllers;
use SoliDry\Extension\BaseController;
class DefaultController extends BaseController
{
protected $jsonApi = false;
}
'custom_sql' => [
'article' => [
'enabled' => true,
'query' => 'SELECT id, title FROM article a INNER JOIN tag_article ta ON ta.article_id=a.id
WHERE ta.tag_id IN (
SELECT id FROM tag WHERE CHAR_LENGTH(title) > :tag_len
) ORDER BY a.id DESC',
'bindings' => [
'tag_len' => 5,
]
],
],
protected $fillable = [
'id',
'title'
];
namespace Modules\V1\Http\Controllers;
use Illuminate\Http\Request;
class ArticleController extends DefaultController
{
public function create(Request $request)
{
// any business logic here for input pre-processing data
parent::create($request);
// any business logic here for output pre-processing data
}
}
namespace Modules\V1\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use SoliDry\Extension\BaseController;
class DefaultController extends BaseController
{
public function __construct(Route $route)
{
// specific code before init
parent::__construct($route);
// specific code after init
}
public function index(Request $request)
{
// specific code before index execution
parent::index($request);
// specific code after index execution
}
}
namespace Modules\V2\Http\Controllers;
class ArticleController extends DefaultController
{
private $prop = 'foo';
// >>>props>>>
// <<<props<<<
public function myMethod()
{
return true;
}
}
namespace Modules\V2\Http\Requests;
use SoliDry\Extension\BaseFormRequest;
class TagFormRequest extends BaseFormRequest
{
public $userPropOne = true;
// >>>props>>>
public $id = null;
// Attributes
public $title = null;
// <<<props<<<
public $userPropTwo = 123;
public function userDefinedMethod(): int
{
return 1;
}
// >>>methods>>>
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
"title" => "string|
namespace Modules\V1\Entities;
use SoliDry\Extension\BaseModel;
class Article extends BaseModel
{
public $userPropOne = true;
// >>>props>>>
protected $primaryKey = "id";
protected $table = "article";
public $timestamps = false;
// <<<props<<<
public $userPropTwo = 123;
public function userDefinedMethod(): int
{
return 1;
}
// >>>methods>>>
public function tag()
{
return $this->belongsToMany(Tag::class, 'tag_article');
}
public function topic()
{
return $this->belongsTo(Topic::class);
}
// <<<methods<<<
public function anotherUserDefinedMethod(): bool
{
return false;
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnLastNameToUser extends Migration
{
public function up()
{
Schema::table('user', function(Blueprint $table) {
$table->string('last_name', 256);
$table->index(['first_name', 'last_name']);
$table->unsignedBigInteger('permissions');
});
}
public function down()
{
Schema::table('user', function(Blueprint $table) {
$table->dropColumn('last_name');
$table->dropColumn('permissions');
});
}
}