REST
application on the Silex
PHP framework, starting with installing Silex
and ending with creating several application routes.Silex
, but let's use Composer
for this. composer require silex/silex ~1.1
require
adds a new package to the project.silex / silex
package name in the format vendor (vendor) / package (package)~ 1.1
- package version, ~ 1.1 means> = 1.1, <2.0 toyshop/ composer.json composer.lock vendor/ ...
{ "require": { "silex/silex": "~1.1" } }
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.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();
/
and /{stockcode}
JSON
and return the encoded data as a response to the request.$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 {./{stockcode}
stockcode
parameter. As you can see, this is a function in this route with a slightly more complicated header.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.toyshop toyshop/00001
toyshop toyshop/00001
and see the result that each of these queries returns.toyshop/00001
toyshop/00001
will return: {"name":"Raspberry Pi","quantity":"13","description":"...","image":"raspberry_pi.jpg"}
$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) { //... });
Source: https://habr.com/ru/post/208206/
All Articles