Slim is a full-featured,open-source PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. It comes with a sophisticated URL dispatcher and middleware architecture that makes it ideal for static websites or API prototyping. It supports all(GET, POST, PUT, DELETE) the HTTP methods.
This article examines Slim in detail, illustrating how you can use it to rapidly build and deploy a REST API with support for authentication and multiple request/response formats.
How to install
If you are using Composer, the PHP dependency manager, simply issue the following command
Replace [my-app-name] with the desired directory name for your new application. The above command will create a project using Slim-Skeleton application and it has below show directory structure.
Now You can run it with PHP’s built-in webserver or you can point your browser with full URL.
So after gone through the above steps, if you point your browser to
http://locahost:8080/
, you would have following output in the browser –Database design and table
Name your database whatever you want, and run below shown SQL it will create task table.
Insert some sample data into the tasks table.
Database configuration
Open your
src/settings.php
file and configure your database setting by adding/editing below showing database config array.Now open your
src/dependencies.php
file and configure database library. There are many database libraries available for PHP, but this example uses PDO – this is available in PHP as standard so it’s probably useful in every project, or you can use your own libraries by adapting the examples below.In the below code we are injecting database object into container using dependicy injection, in this case called db:
We are going to implement following API calls –
Method | URL | Action |
---|---|---|
GET | /todos | Retrieve all todos |
GET | /todos/search/bug | Search for todos with ‘bug’ in their name |
GET | /todo/1 | Retrieve todo with id == 1 |
POST | /todo | Add a new todo |
PUT | /todo/1 | Update todo with id == 1 |
DELETE | /todo/1 | Delete todo with id == 1 |
Implementing the API calls with Slim
Now that we have our Slim app up and running with database connection, we need to manage todos in the database.
Getting the Todos list – We are going to create a new route so that when a user hits /todos, it will return a list of all todos in JSON format. Open your
src/routes.php
and addThis function simply return all todos information as you can see in this query, to call this API use this URL
http://localhost:8080/todos
.Getting single todo – We are going to create a new route so that when a user hits /todo/{id}, it will return a todo in JSON format.
This function check record of given id and return if found anything, to call this API use this URL
http://localhost/todo/1
.Find todo by name – We are going to create a new route so that when a user hits /todos/search/{Query}, it will return a list of all matched todos in JSON format.
This function search in database for your given query, to call this API use this URL
http://localhost/todos/search/bug
Add todo – We are going to create a new route so that when a user sends a post request to /todo with required data, app will add a new record to the database.
This API accept post request and insert submitted data in your database. To call this API use this URL
http://localhost/todo
Delete Task – We are going to create a new route so that when a user sends a delete request to /todo/{id}, app will delete a record from the database.
Update Task – We are going to create a new route so that when a user sends a put request to /todo/{id} with required data, app will updated a record based on match parameter in the database.
This API accept put request and updates submitted data in your database. To call this API use this URL
http://localhost/todo/{id}
Here is the the complete final
src/routes.php
fileI hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
No comments:
Post a Comment