1
Slim is a micro web framework like Flask
or Sinatra
but for PHP. Eloquent ORM
is a powerful database toolkit and it serves as a database layer of the Lravel
PHP framework. Now, this time i will install Elequent ORM
in Slime
framework and build some Restful API with it. Let’s get started!
Here is the part of composer.json
in my project:
1 | "require": { |
In my project, i use the illuminate/database 4.2
because my PHP version is 7.0 which is not support some feature like this:
1 | [$foo, $bar] = [1,2]; |
But if you have higher PHP version you can try the latest Eloquent
.
Run the composer install
to install the repo into the project.
Config Eloquent
After downloading the Eloquent we should configure it so that our application will know about it. Open the src/config.php
and add databse setting array:
1 | 'db' => [ |
boost Eloquent
now instantiate the capsule manager and boost Eloquent in public/index.php
:
1 | $dbSettings = $container->get('settings')['db']; |
create Model
OK. Every thing has done, Let’s create my database model. Create src/app/model/Goods.php
file and edit:
1 | namespace app\model; |
Class Goods
extends Illuminate\Database\Eloquent\Model
so it can retrieve and store data from our databse. The $table
property assigns which data table this model will use.
write restful API
1 | namespace app\controller; |
Edit the src/routes.php
:
1 | $app->get('/goods', 'app\controller\GoodsController:index'); |
That’s works! Happy Hacking!