Let's Build an API using Slim PHP Framework
HTML-код
- Опубликовано: 1 май 2024
- This video shows you how to build an API using the Slim PHP microframework. The API allows you to add, delete, modify, and fetch the details of cricket players via JSON HTTP requests. We'll also implement data validation, authentication, and error handling using middlewares.
GitHub repo link: github.com/iabhinavr/slim-php...
0:00 Intro
1:53 Installing Composer
5:53 Starting with the index.php file
8:38 Setting up the server
16:13 Installing Postman
17:50 Adding Routes
19:18 Connecting to the Database
22:25 Routes continued...
23:22 Using DBAL for Database Queries
27:42 Understanding Dependency Injection
31:03 Route to get all players
32:22 Route to get a single player
34:50 Route to add a new player
35:33 Getting input data using Body Parser Middleware
41:41 Route to update a player
44:23 Route to delete a player
45:43 Authentication
54:40 Request data validation
1:01:05 Handling Errors
1:03:10 Storing credentials in dotenv file
1:05:51 Final Notes
Website: codingreflections.com
---------------------------------------
Server providers:
Linode: codingreflections.com/go/linode/
DigitalOcean: codingreflections.com/go/digi...
-------------------------------------------------------------
Disclaimer:
This video is based on my personal experiences and knowledge. There is no guarantee that the information shared is 100% accurate.
Disclosure:
This video and its description may contain affiliate links, which can earn us commission if someone purchases products using those links. - Наука
Thank you, great content
gracias por compartir, explicas bastante bien 👍
I get error name to resolved. I am running it in WSL so maybe thats the issue
👏🏻👏🏻👏🏻👏🏻👏🏻
Thank you for the video. You did explain things very well. Now it is time to try to build a REST API myself. One question came to mind directly. Is it not possible to use the env file for the error handling settings?
in Error Handling section, showing an html output of an error is not a good use. that should be json formatted error response...
Thanks for the video. There is only one thing I didn't understand: how is it possible to do $this->get('DB') in the api.php file to get the queryBuilder since you are not in a class and therefore there are no properties? Thank you
$this keyword is used inside a callback function within the $app->get() call. It is something called closure or anonymous function in PHP, not a traditional class method. $app being an instance of the Slim app, $this inside the callback refers to that. That's what I've understood.
Great tutorial thank you for this.
Please I am getting this error from the public/index.php:
Fatal error: Uncaught TypeError: Argument 1 passed to Slim\Factory\AppFactory::setContainer() must be an instance of Psr\Container\ContainerInterface, instance of DI\Container given
In fact the whole code from the start of PHP-DI was very vague and fails. I just followed it like a robot. I think the whole video from this point seems confusing. I may retry from the start and if it fails i will just dump it. To make it worse, Slim and PHP-DI dump cryptic messages which would beat the ancient Sumerian scripts.
@@truthteachers so I actually resolved this issue by installing php-di/slim-bridge library.
I think this is the new way to work with php di in the slim framework.
You could try cloning the Github repo given in the description and see if that's working. I've updated the composer packages.