1. Go to this page and download the library: Download napp/apicore 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/ */
namespace App\Posts\Factory;
use App\Posts\Models\Post;
use Napp\Core\Api\Validation\ValidateTrait;
class PostFactory
{
use ValidateTrait;
/**
* @param array $attributes
* @param bool $validate
* @return Post
* @throws \Napp\Core\Api\Exceptions\Exceptions\ValidationException
*/
public static function create(array $attributes, $validate = true): Post
{
if (true === $validate) {
static::validate($attributes, PostValidationRules::$createRules);
}
return new Post($attributes);
}
}
namespace App\Posts\Request;
use App\Posts\Factory\PostValidationRules;
use App\Posts\Transformer\PostTransformer;
use Napp\Core\Api\Requests\ApiRequest;
use Napp\Core\Api\Transformers\TransformerInterface;
class StorePostRequest extends ApiRequest
{
/**
* @return array
*/
public function rules(): array
{
return PostTransformer::$createRules;
}
/**
* @return TransformerInterface
*/
protected function getTransformer(): TransformerInterface
{
return app(PostTransformer::class);
}
}
namespace App\Posts\Controllers\Api;
use App\Posts\Factory\PostFactory;
use App\Posts\Transformer\PostTransformer;
use Napp\Core\Api\Controllers\ApiController;
class PostController extends ApiController
{
public function show(int $id, Request $request, PostTransformer $transformer): JsonResponse
{
$post = $this->postRepository->find($id);
if (null === $post) {
return $this->responseNotFound();
}
return $this->respond($transformer->transformOutput($post));
}
public function store(StorePostRequest $request, PostTransformer $transformer): JsonResponse
{
if (/* some logic */) {
return $this->responseUnauthorized();
}
$post = PostFactory::create($request->validated(), false);
return $this->responseCreated($transformer->transformOutput($post));
}
}
use Napp\Core\Api\Controllers\ApiInternalController;
class MyController extends ApiInternalController
{
public function someImportantAction()
{
// using API get/post/put/delete
$data = $this->get('/api/v1/some/route');
$stuff = $this->post('/api/v1/new/stuff', $data);
return view('my.view', compact('stuff'));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.