mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-06-08 18:57:12 +00:00
Rename all file to kebab-case
This commit is contained in:
parent
ffc085d78a
commit
68ca4e4f86
117 changed files with 0 additions and 1260 deletions
97
php/simple-mvc/rest-api.md
Normal file
97
php/simple-mvc/rest-api.md
Normal file
|
@ -0,0 +1,97 @@
|
|||
# REST API with Simple-MVC
|
||||
|
||||
## Routing (Example)
|
||||
|
||||
```php
|
||||
// config/route.php
|
||||
return [
|
||||
[ 'GET', '/api/user[/{id}]', Controller\User::class ],
|
||||
[ 'POST', '/api/user', Controller\User::class ],
|
||||
[ 'PATCH', '/api/user/{id}', Controller\User::class ],
|
||||
[ 'DELETE', '/api/user/{id}', Controller\User::class ]
|
||||
];
|
||||
```
|
||||
|
||||
## Controller (Example)
|
||||
|
||||
```php
|
||||
public class UserController implements ControllerInterface
|
||||
{
|
||||
public function __construct(UserModel $user)
|
||||
{
|
||||
$this->userModel = $user;
|
||||
|
||||
// Set the Content-type for all the HTTP methods
|
||||
header('Content-type: application/json');
|
||||
}
|
||||
|
||||
// method dispatcher
|
||||
public function execute(ServerRequestInterface $request)
|
||||
{
|
||||
$method = strtolower($request->getMethod());
|
||||
if (!method_exists($this, $method)) {
|
||||
http_response_code(405); // method not exists
|
||||
return;
|
||||
}
|
||||
$this->$method($request);
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
try {
|
||||
$result = empty($id)
|
||||
? $this->userModel->getAllUsers()
|
||||
: $this->userModel->getUser($id);
|
||||
} catch (UserNotFoundException $e) {
|
||||
http_response_code(404); // user not found
|
||||
$result = ['error' => $e->getMessage()];
|
||||
}
|
||||
echo json_encode($result);
|
||||
}
|
||||
|
||||
public function post(ServerRequestInterface $request)
|
||||
{
|
||||
$data = json_decode($request->getBody()->getContents(), true);
|
||||
try {
|
||||
$result = $this->userModel->addUser($data);
|
||||
} catch (InvalidAttributeException $e) {
|
||||
http_response_code(400); // bad request
|
||||
$result = ['error' => $e->getMessage()];
|
||||
} catch (UserAlreadyExistsException $e) {
|
||||
http_response_code(409); // conflict, the user is not present
|
||||
$result = ['error' => $e->getMessage()];
|
||||
}
|
||||
echo json_encode($result);
|
||||
}
|
||||
|
||||
public function patch(ServerRequestInterface $request)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
$data = json_decode($request->getBody()->getContents(), true);
|
||||
try {
|
||||
$result = $this->userModel->updateUser($data, $id);
|
||||
} catch (InvalidAttributeException $e) {
|
||||
http_response_code(400); // bad request
|
||||
$result = ['error' => $e->getMessage()];
|
||||
} catch (UserNotFoundException $e) {
|
||||
http_response_code(404); // user not found
|
||||
$result = ['error' => $e->getMessage()];
|
||||
}
|
||||
echo json_encode($result);
|
||||
}
|
||||
|
||||
public function delete(ServerRequestInterface $request)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
try {
|
||||
$this->userModel->deleteUser($id);
|
||||
$result = ['result' => 'success'];
|
||||
} catch (UserNotFoundException $e) {
|
||||
http_response_code(404); // user not found
|
||||
$result = ['error' => $e->getMessage()];
|
||||
}
|
||||
echo json_encode($result);
|
||||
}
|
||||
}
|
||||
```
|
146
php/simple-mvc/simple-mvc.md
Normal file
146
php/simple-mvc/simple-mvc.md
Normal file
|
@ -0,0 +1,146 @@
|
|||
# [SimpleMVC](https://github.com/ezimuel/simplemvc) Mini-Framework
|
||||
|
||||
SimpleMVC is a micro MVC framework for PHP using [FastRoute][fastroute], [PHP-DI][php-di], [Plates][plates] and [PHP-DI][php-di] standard for HTTP messages.
|
||||
|
||||
This framework is mainly used as tutorial for introducing the Model-View-Controller architecture in modern PHP applications.
|
||||
|
||||
[php-di]: https://php-di.org/
|
||||
[fastroute]: https://github.com/nikic/FastRoute
|
||||
[psr7]:https://github.com/Nyholm/psr7
|
||||
[plates]: https://platesphp.com/
|
||||
|
||||
## Installation
|
||||
|
||||
```ps1
|
||||
composer create-project ezimuel/simple-mvc
|
||||
```
|
||||
|
||||
## Structure
|
||||
|
||||
```txt
|
||||
|- config
|
||||
| |- container.php --> DI Container Config (PHP-DI)
|
||||
| |- route.php --> routing
|
||||
|- public
|
||||
| |- img
|
||||
| |- index.php --> app entry-point
|
||||
|- src
|
||||
| |- Model
|
||||
| |- View --> Plates views
|
||||
| |- Controller --> ControllerInterface.php
|
||||
|- test
|
||||
| |- Model
|
||||
| |- Controller
|
||||
```
|
||||
|
||||
### `index.php`
|
||||
|
||||
```php
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
chdir(dirname(__DIR__));
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use DI\ContainerBuilder;
|
||||
use FastRoute\Dispatcher;
|
||||
use FastRoute\RouteCollector;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Nyholm\Psr7Server\ServerRequestCreator;
|
||||
use SimpleMVC\Controller\Error404;
|
||||
use SimpleMVC\Controller\Error405;
|
||||
|
||||
$builder = new ContainerBuilder();
|
||||
$builder->addDefinitions('config/container.php');
|
||||
$container = $builder->build();
|
||||
|
||||
// Routing
|
||||
$dispatcher = FastRoute\simpleDispatcher(function(RouteCollector $r) {
|
||||
$routes = require 'config/route.php';
|
||||
foreach ($routes as $route) {
|
||||
$r->addRoute($route[0], $route[1], $route[2]);
|
||||
}
|
||||
});
|
||||
|
||||
// Build the PSR-7 server request
|
||||
$psr17Factory = new Psr17Factory();
|
||||
$creator = new ServerRequestCreator(
|
||||
$psr17Factory, // ServerRequestFactory
|
||||
$psr17Factory, // UriFactory
|
||||
$psr17Factory, // UploadedFileFactory
|
||||
$psr17Factory // StreamFactory
|
||||
);
|
||||
$request = $creator->fromGlobals();
|
||||
|
||||
// Dispatch
|
||||
$routeInfo = $dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getUri()->getPath()
|
||||
);
|
||||
switch ($routeInfo[0]) {
|
||||
case Dispatcher::NOT_FOUND:
|
||||
$controllerName = Error404::class;
|
||||
break;
|
||||
case Dispatcher::METHOD_NOT_ALLOWED:
|
||||
$controllerName = Error405::class;
|
||||
break;
|
||||
case Dispatcher::FOUND:
|
||||
$controllerName = $routeInfo[1];
|
||||
if (isset($routeInfo[2])) {
|
||||
foreach ($routeInfo[2] as $name => $value) {
|
||||
$request = $request->withAttribute($name, $value);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
$controller = $container->get($controllerName);
|
||||
$controller->execute($request);
|
||||
```
|
||||
|
||||
### `route.php`
|
||||
|
||||
```php
|
||||
<?php
|
||||
use SimpleMVC\Controller;
|
||||
|
||||
return [
|
||||
[ 'GET', '/', Controller\Home::class ],
|
||||
[ 'GET', '/hello[/{name}]', Controller\Hello::class ],
|
||||
[ "HTTP Verb", "/route[/optional]", Controller\EndpointController::class ]
|
||||
];
|
||||
```
|
||||
|
||||
### `container.php`
|
||||
|
||||
```php
|
||||
<?php
|
||||
use League\Plates\Engine;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
return [
|
||||
'view_path' => 'src/View',
|
||||
Engine::class => function(ContainerInterface $c) {
|
||||
return new Engine($c->get('view_path'));
|
||||
}
|
||||
|
||||
// PHP-DI configs
|
||||
];
|
||||
```
|
||||
|
||||
### `ControllerInterface.php`
|
||||
|
||||
Each controller *must* implement this interface.
|
||||
|
||||
```php
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SimpleMVC\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
interface ControllerInterface
|
||||
{
|
||||
public function execute(ServerRequestInterface $request);
|
||||
}
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue