On php it can be easy and fast too

In my opinion, the php language has always been a pretty good solution for creating a complex backend of web applications, and in the nineties and zero it gained such immense popularity (just huge, comparable to IE for web surfing at that time) primarily due to the ease, speed of development and code support. But those days are over. Today it is believed that php applications have become monstrous, take a long time and are difficult to run, can only work with pulling a lot of dependencies into the / vendor directory ...





, , . API Node.js Go.





, API php.





Slim (https://www.slimframework.com/), / , ORM, CMS, " ".





Slim php. , , (Apache 2, Nginx ), composer, , . (cooking app):





mkdir my-slim-api && cd my-slim-api
composer init
      
      



, composer.json :





{ 
  "name": "zhukmax/slim", 
  "type": "project", 
  "license": "MIT", 
  "autoload": { 
    "psr-4": { 
      "Zhukmax\\Slim\\": "src/" 
    } 
  }, 
  "require": {}
}
      
      



, Slim, http- (PSR-7) http- (PSR-17):





composer require slim/slim:"4.*"
composer require slim/psr7
      
      



API index-, , "-", json :





<?php 
require __DIR__ . '/vendor/autoload.php'; 
 
use Psr\Http\Message\ResponseInterface as Response; 
use Psr\Http\Message\ServerRequestInterface as Request; 
use Slim\Factory\AppFactory; 
 
$app = AppFactory::create(); 
 
$app->get('/', function (Request $request, Response $response, $args) {
  $data = array('name' => 'Max', 'role' => 'web developer');
  $response->getBody()->write(json_encode($data));

  return $response->withHeader('Content-Type', 'application/json'); 
}); 
 
$app->run();
      
      



composer.json , php-json ( – )





"require": {
  "slim/slim": "4.*",
  "slim/psr7": "^1.4",
  "ext-json": "*"
}
      
      



MySQL

Slim , ORM. Laravel Eloquent Doctrine, NoSQL . Idiorm .





composer require j4mie/idiorm
      
      



:





<?php

ORM::configure([
  //  ,  localhost    ,
  //     127.0.0.1

  'connection_string' => 'mysql:host=127.0.0.1;dbname=mydb', 
  'username' => 'root', 
  'password' => '124' 
]);
 
$app->get('/users', function (Request $request, Response $response, $args) { 
  $users = ORM::forTable("users")->find_array();
  $response->getBody()->write(json_encode($users));

  return $response;
});

      
      



, url, users json-.





, ,

, , , . GET, POST, PUT DELETE http- .





<?php

$app->post('/users', function (Request $request, Response $response, $args) { 
  $parsedBody = $request->getParsedBody(); 
 
  $user = ORM::forTable("users")->create(); 
  $user->name = $parsedBody['name'] ?? ''; 
 
  if ($user->save()) { 
    $successRes = $response->withStatus(201); 
    $successRes->getBody()->write(json_encode([ 
      "message" => "Success" 
    ])); 
 
	  return $successRes; 
  } else { 
    $errorRes = $response->withStatus(501); 
    $errorRes->getBody()->write(json_encode([ 
      "message" => "Error" 
    ]));
    
    return $errorRes; 
  } 
});
      
      



, , , , . , , , , . , - ORM. Idiorm , , . API, , .





actions

index.php – , . -.





$app->get('/users/{id}', [UserController::class, "getOne"]);
      
      



Slim – action , . , -, , , . . , .





<?php

class UserController 
{ 
  public function getOne(Request $request, Response $response, $args): Response 
  { 
    $id = (int)$args['id'] ?? 0; 
    $user = ORM::forTable("table1")->findOne($id); 
 
    if (!$user) { 
      return self::errorResponse($response, 404, "Error text"); 
    } 
 
    $response->getBody()->write(json_encode([ 
      "id" => $user->id, 
      "name" => $user->name 
    ])); 
 
    return $response; 
  }
 
  private static function errorResponse(Response $response, int $code, string $text): Response 
  { 
    $errorRes = $response->withStatus($code); 
    $errorRes->getBody()->write(json_encode([ 
      "message" => $text 
    ])); 
 
    return $errorRes; 
  }
}
      
      



, . php API, Express, Angular - backend. , . , . , web-.





, github (https://github.com/ZhukMax/slim-api), .








All Articles