Welcome to the fastest & easiest REST-API starter project

current controller: Example/Routes/Home.php

What's included?

  • JWT authentication
  • Attribute routing
  • ORM handler

In order to see the simplicity of handling requests, please have a look at the file src/Example/Api/Example.php
The endpoint accepts the methods POST, GET, PUT & DELETE (via attributes)

Quick start

Creating new endpoints

  • Run
    php cli create:controller App\Movies\Routes\GetMovies
    in your terminal. (On unix systems, you will have to use 2 backslashes)
  • Add the attribute
    #[\Neoan\Routing\Attributes\Get('/api/movies')]
    to the generated class (src/Movies/GetMovies/GetMovies.php).
  • Visit /api/movies
  • Let's create a POST-endpoint as well
    php cli create:controller App\Movies\Routes\CreateMovie
  • And route it via
    #[\Neoan\Routing\Attributes\Post('/api/movies')]
  • Scaffold-command: This project is equipped with a command shortening the recommended structure.
    php cli scaffold:route get Movie GetMovies
    php cli scaffold:route post Movie CreateMovies

Creating a new model

  • Run
    php cli create:model App\Movies\Models\Movie
    in your terminal. (On unix systems, you will have to use 2 backslashes)
  • Open the file src/Movies/Models/Movie.php and add two properties:
    • public string $name;
    • public string $director;
  • Then, migrate all models
    php cli migrate:models sqlite
  • Scaffold-command: This project is equipped with a command shortening the recommended structure.
    php cli scaffold:model Movie

Creating a new request (guard)

  • Run
    php cli create:request App\Movies\Requests\WriteMovieRequest
    in your terminal. (On unix systems, you will have to use 2 backslashes)
  • Open the file src/Movies\Requests\WriteMovieRequest.php and add two properties:
    • public string $name;
    • public string $director;
    In our simple example, this allows the same request properties as the model.
  • Scaffold-command: This project is equipped with a command shortening the recommended structure.
    php cli scaffold:request Movie WriteMovieRequest

Connecting the dots

Let's revisit our CreateMovie-controller and apply the following changes:
  • First, let's add our request guard to the invoke method
    public function __invoke(\App\Movie\Requests\WriteMovieRequest $request): array
  • Let's also change the return type of the method
    public function __invoke(\App\Movie\Requests\WriteMovieRequest $request): array \App\Movie\Models\Movie
  • Now, instead of the existing content
    return ['name' => 'CreateMovie'];
    , we create or update a new movie
    return \App\Movie\Models\Movie::retrieveOneOrCreate((array) $request)->store();

Now let's create one or two movies

  • Finally, we can expose our movies through our GetMovies-controller
    return ['name' => 'GetMovies'];
  • return \App\Movie\Models\Movie::retrieve()->toArray();
  • That's it! Let's try it: /api/movies. Or with the self-documenting API: API

Ready for the deep-dive?

This starter is based on neoan.io LENKRAD. You can use this code under MIT license in any project of your choice.