In this tutorial, we will create a Mini Blog API using Lithe, a PHP framework that is simple and flexible, and Eloquent ORM, which facilitates interaction with the database. Lithe already configures Eloquent automatically, so the work we need to do is simple: adjust the environment variables and create the model and migration for our data.
Let's follow a simple step-by-step guide to build this API efficiently and without complications.
The first step is to install Lithe in your project. If you haven't done this yet, just run the following command in the terminal:
composer create-project lithephp/lithephp mini-blog-api
This command creates a new project using Lithe. Lithe already configures Eloquent for you automatically, but we need to adjust some settings in the .env file to connect to the database.
Now, let's configure the database. Open the .env file in the root of your project and edit the database settings. To use Eloquent ORM with MySQL, the settings should be like this:
DB_CONNECTION_METHOD=eloquent DB_CONNECTION=mysql DB_HOST=localhost DB_NAME=lithe_eloquent DB_USERNAME=root DB_PASSWORD= DB_SHOULD_INITIATE=true
Now, since Lithe already automatically configures Eloquent, the next step is to ensure that Eloquent ORM is installed. If you haven't already done so, run the command below to install Eloquent ORM:
composer require illuminate/database
After installation, Lithe will be ready to use Eloquent ORM and interact with the database. With this, the database is correctly configured for our Mini Blog!
APINow, let's create the model and migration that will define the posts table in our database.
First, create the Post template with the command:
php line make:model Post
Then, create the migration for the posts table:
php line make:migration create_posts_table
The model and migration have already been created, now let's configure them.
The Post model is located in src/models/Post.php. Edit the file like this:
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { // A tabela associada ao modelo protected $table = 'posts'; // Campos que podem ser preenchidos via mass-assignment protected $fillable = ['title', 'content']; // Usar timestamps para created_at e updated_at public $timestamps = true; }
In this code, we are defining the title and content fields as fillable, meaning they can be filled automatically when we create or update a post.
The generated migration will be in src/database/migrations/{timestamp}_create_posts_table.php. Edit the migration to create the posts table structure:
composer create-project lithephp/lithephp mini-blog-api
Here, we are creating the posts table with the fields id, title, content, and the date and time fields created_at and updated_at.
With the migration and model ready, let's run the migration to create the posts table in the database. Run the following command:
DB_CONNECTION_METHOD=eloquent DB_CONNECTION=mysql DB_HOST=localhost DB_NAME=lithe_eloquent DB_USERNAME=root DB_PASSWORD= DB_SHOULD_INITIATE=true
This command will create the posts table in the database with the fields that we defined in the migration.
Now, let's create a controller to manage the API posts. The controller will be responsible for handling HTTP requests and returning data in an organized manner.
To create the controller, run:
composer require illuminate/database
This will generate a file at src/http/Controllers/PostController.php. Edit this file to include CRUD (create, read, update and delete) methods for posts.
Here is an example of what the PostController might look like:
php line make:model Post
Here, we have five basic methods:
Now, let's define the routes for our posts API. Open the src/App.php file and add the following code:
php line make:migration create_posts_table
The code above creates an instance of the Lithe application. The line $app->set('routes', __DIR__ . '/routes'); tells Lithe where to find the route files. Lithe will automatically load all files into the src/routes folder. Each route file will be mapped to the URL based on its name. For example:
The $app->listen(); causes Lithe to start "listening" to requests, that is, it waits for requests and directs them to the defined routes.
Now, create a file called posts.php inside the src/routes/posts folder to represent the /posts route and add the following code:
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model { // A tabela associada ao modelo protected $table = 'posts'; // Campos que podem ser preenchidos via mass-assignment protected $fillable = ['title', 'content']; // Usar timestamps para created_at e updated_at public $timestamps = true; }
These routes connect PostController methods to API URLs.
Now that everything is set up, you can test your API with tools like Postman or Insomnia. Here are the endpoints you can test:
Now, you've just created a Mini Blog API with Lithe and Eloquent ORM! Lithe automatically configured Eloquent for you, and we just made some modifications to the environment variables and created the necessary models and controllers. Now you have a complete RESTful API for managing blog posts.
For more information and details on how to use Lithe and Eloquent ORM, visit the official Lithe documentation here: Lithe Documentation.
The above is the detailed content of Creating a Mini Blog API with Lithe and Eloquent. For more information, please follow other related articles on the PHP Chinese website!