Plans and First Steps | Creating a RESTful API with ReactPHP
In the last video we have covered theory about RESTful APIs, but now let's start with building a RESTful API with ReactPHP. Let's have a look at what we will build. Our API will have a couple of resources. I want it to be realistic. Let's say that we have "/products" resource. This URL supports GET request to receive a list of all products and POST request to create a new product. Also, we want to target a single product by its id, so we have "/products/{id}". And this URL supports GET request to get information about a specific product, PUT request to update that product, to change some data, and DELETE request to delete the product. Also, we will implement one more resource "/orders". We are going to support GET request to retrieve all orders, POST request to create a new order. And we are going to have access to a specific order by its id. So, we need URL "/orders/{id}". We will implement GET request to get more information about an order, we are not going to update any order,so there will be no PUT requests. But we will allow to cancel or to DELETE an order. That is what we are going to build. It is our RESTful API. And we also are going to add authentication to make sure that some of these routes are protected. Only an authenticated user is allowed to call them. And we will also learn how to log in user when using RESTful API. Because as we have previously learned there is no session on the server. So let's start right now. Here I have an empty project. First of all, we need to install a couple of dependencies. We need react/http. This package provides tools to create an HTTP server on PHP. We will add more packages through this video series but for now, we start with this one. With react/http being installed we create a new PHP file server.php. Here we will set up all the code for running our asynchronous HTTP server. Now let's create and run our simple server. Well, first of all, we require Composer autoloader. The first and the most important thing we need to do is to create an event loop. It is the heart of any asynchronous ReactPHP application. Then we instantiate a server. When we create a server we need to provide a middleware, a callable request handler which is executed whenever we got a new request and then it returns a response. I'll live it empty now, but we will add it soon. Actually, this code now wouldn't work, because there is no request handler here. Then I open a socket on localhost, port 8000. And the server starts listening for incoming connections on this socket. This is a very simple server set up. Print a friendly message saying that the server is running. And at the end of the script we start the loop. This line is the place where our program, our server actually starts running. Now we come back here, to server constructor and we need to provide a request handler, a middleware. What is middleware? The incoming request has to go through this request handler. It can have different formats: instance of a callable class, or a function, where you get the request and some special variable called $next which is actually also a callable. You can execute this callable to move the request to the next middleware and if you don't execute it the request will not go there. In our case we have just one middleware, so there will be no next request handlers. Here we directly send a response. Let's send a JSON response. We set a status code, 200 saying that everything is OK, then we provide headers telling that the client should consider the response body as JSON, and we provide a json-encoded string. We send an object with property "message" saying that our server works. So, having that we have a server set up that should actually work. This server allows us to send any type of request and receive back a JSON response. Let's try it out. We run the script. The event loop starts running, you see that the process doesn't finish, it should keep on running. And how we can see that it works? Let's try to send GET request from PHPStorm to localhost port 8000. You see we have a response with status code 200, and a JSON object. Now let's try POST request and you should also see the message. And with that, we have created our first very naive RESTful API implementation. It is not really useful and it doesn't have any endpoints, so let's continue on and improve it.
What is a RESTful API? | Creating a RESTful API with ReactPHP ReactPHP Tutorial #5: Simple Chat With Sockets - Client ReactPHP Tutorial #2: Streams ReactPHP Tutorial #3: Promises ReactPHP Tutorial #8: Query Strings ReactPHP Tutorial #10: Middleware ReactPHP Tutorial #1: Event Loop And Timers ReactPHP Tutorial #9: POST Requests Introduction to ReactPHP and asynchronous PHP ReactPHP Tutorial #6: Simple Chat With Sockets - Making it user-friendly