📜 ⬆️ ⬇️

Creating a simple REST application on Silex

In this article, we will create a simple REST application on the Silex PHP framework, starting with installing Silex and ending with creating several application routes.
Silex is a PHP micro framework from the creators of symfony and in many respects built on its base. More information about it can be found in the following habraposta


Let's start


For this example, I will use a directory called toyshop

Silex installation


There are several ways to install Silex , but let's use Composer for this.
Inside your working directory, run the command:
')
 composer require silex/silex ~1.1 


With this command, you gave the Composer command to do the following:


When you run this command, you will see that Composer will also install libraries that you did not request - do not worry, it should have happened, since Silex requires these libraries to work correctly.

As soon as the command finishes, you will receive the following file structure, as shown below:
 toyshop/ composer.json composer.lock vendor/ ... 


All Silex code is stored inside the vendor folder, and 2 Composer files contain information about which packages and versions of libraries are installed.

Composer.json


Let's look inside composer.json, it should look something like this:
 { "require": { "silex/silex": "~1.1" } } 


Composer.lock


This file contains a list of all installed packages, as well as their versions. This is very useful, because it means that you no longer have to commit folders to the vendor/ when you upload a project to the repository - all the libraries needed for the application to work correctly can be installed based on this file. You just need to upload the composer.json and composer.lock files into the repository and then, as soon as the code is retrieved from the repository, you can simply run the composer install command and Composer install the necessary libraries of the specified versions.

Creating routes



Since our application is a toy store, let's think about what we need. People who will come to our site will want to see the gifts, so let's create a route that will allow you to receive a list of gifts.

Let's create the index.php file. Add code to it so that it looks like this:
 <?php require_once __DIR__.'/vendor/autoload.php'; $app = new Silex\Application(); // Please set to false in a production environment $app['debug'] = true; $toys = array( '00001'=> array( 'name' => 'Racing Car', 'quantity' => '53', 'description' => '...', 'image' => 'racing_car.jpg', ), '00002' => array( 'name' => 'Raspberry Pi', 'quantity' => '13', 'description' => '...', 'image' => 'raspberry_pi.jpg', ), ); $app->get('/', function() use ($toys) { return json_encode($toys); }); $app->get('/{stockcode}', function (Silex\Application $app, $stockcode) use ($toys) { if (!isset($toys[$stockcode])) { $app->abort(404, "Stockcode {$stockcode} does not exist."); } return json_encode($toys[$stockcode]); }); $app->run(); 


Now we have 2 routes announced: / and /{stockcode}

Route /


This is the route to get a list of all the toys. All that this method does is encode the data in JSON and return the encoded data as a response to the request.

To allow an anonymous function to access the $toys array, we must import it by adding use($toys) at the end of the definition of an anonymous function, but before opening the bracket {.

Route /{stockcode}


This is the route to get information about a particular toy, which is identified by the stockcode parameter. As you can see, this is a function in this route with a slightly more complicated header.

Along with use($toys) we pass a couple of variables to the function itself: Silex\Application $app $stockcode.

Silex\Application $app is a Silex application object and with its help we can output a beautiful 404 error page if an incorrect stockcode has been transmitted.

$stockcode is the ID of the toy we want to receive information about. As you can see, this variable corresponds to the {stockcode} that is in the route we described.

Testing the application



After configuring the web server to route all requests through index.php, you can navigate to the routes using toyshop toyshop/00001 toyshop toyshop/00001 and see the result that each of these queries returns.

For example, toyshop/00001 toyshop/00001 will return:
 {"name":"Raspberry Pi","quantity":"13","description":"...","image":"raspberry_pi.jpg"} 


The answer returns in a convenient format suitable for use in your online store or mobile applications. All that needs to be done is to perform the json_decode function on these data and display the data in a convenient format, for example, HTML.

Why Silex?


Good question.
Answer: since this is a micro-framework, it does not force me to write in my own preferred style and it turns out I don’t need to change my programming style to work with Silex. If you wanted, you could use Slim, any other micro-framework, or even full frameworks like Symfony or Zend.

Notes from the translator


I would like to add that it is also easy to work in Silex with other types of HTTP requests, such as PUT, POST, DELETE (example below)
 $app->post('/toys', function (Silex\Application $app) use ($toys) { //... }); $app->put('/{stockcode}', function (Silex\Application $app, $stockcode) use ($toys) { //... }); $app->delete('/{stockcode}', function (Silex\Application $app, $stockcode) use ($toys) { //... }); 


Thus, you can create a full-fledged REST application.

Source: https://habr.com/ru/post/208206/


All Articles