Slim-Doctrine-Demo
Slim-Doctrine-Demo copied to clipboard
Slim-Doctrine-Demo
A project to demonstrate how to integrate Doctrine 3.x into Slim. This is the companion code for the Cookbook entry Using Doctrine with Slim in Slim's documentation.
Requirements
- PHP 8.2+
- PHP SQLite extension
- Composer
Overview
The Slim-Doctrine-Demo project is a small REST API that allows its clients to create and retrieve lists of users.
- GET /users -> Retrieves a list of all users created so far.
- POST /users -> Creates a new user (does not accept any parameters, fake data is autogenerated).
At its core, Doctrine's EntityManager
is used to persist and retrieve these
users from an SQLite database.
Project structure
Slim-Doctrine-Demo
├── .github
│ └── workflows
│ └── ci.yml -- Automated testing pipeline
├── bin
│ └── doctrine -- Doctrine CLI tool
├── public
│ └── index.php -- HTTP front controller (requires ../bootstrap.php)
├── src
│ ├── Action -- Slim request handlers
│ │ ├── CreateUser.php
│ │ └── ListUsers.php
│ ├── DI
│ │ ├── Doctrine.php -- EntityManager service definition
│ │ └── Slim.php -- Slim request handlers service definitions
│ └── Domain -- Annotated entity classes
│ └── User.php
├── tests/ -- Automated tests
├── var
│ ├── coverage/ -- Test coverage results in HTML
│ ├── doctrine/ -- Doctrine metadata cache
│ └── database.sqlite -- Development database
├── bootstrap.php -- DI container setup (requires ./settings.php)
├── composer.json
├── composer.lock
├── LICENSE
├── phpunit.xml.dist
├── README.md -- You are here.
├── settings.php -- Settings currently in use (ignored by Git)
└── settings.php.dist -- Sample settings.php for development (committed to Git)
Running the app
Typing composer serve
in a console will install the project dependencies, create the database and open
the API at http://localhost:8000
. Once it is running you can make requests against it with a browser,
curl or similar tools.
$ curl -X POST localhost:8000/users
{
"id": 2,
"email": "[email protected]",
"registered_at": "2022-01-15T23:10:51+01:00"
}
$ curl -X GET localhost:8000/users
[
{
"id": 1,
"email": "[email protected]",
"registered_at": "2022-01-15T23:10:50+01:00"
},
{
"id": 2,
"email": "[email protected]",
"registered_at": "2022-01-15T23:10:51+01:00"
}
]
Using the Doctrine Command Line Interface
Run ./bin/doctrine
(this script needs the project dependencies, so run composer install
first).
Running the tests
Similarly, typing composer test
will take care of loading the testing environment and running PHPUnit. If the XDebug exension is enabled code coverage results will be available at var/coverage/
after running the tests.