PHP code example of sevval42 / spade

1. Go to this page and download the library: Download sevval42/spade library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sevval42 / spade example snippets


$router->addRoute('GET', '/users/{id:\d+}', [UserController::class, 'handle']);

public function handle(int $id): Response
{
    ...
}

public function handle(Request $request): Response
{
    $method = $request->getMethod();
    $uri = $request->getUri();

    try {
        return $this->router->dispatch($method, $uri);
    } catch (RouteNotFoundException $exception) {
        return new NotFoundResponse();
    } catch (RouteNotAllowedException $exception) {
        return new InternalServerErrorResponse();
    }
}

$connection->query('SELECT * FROM user WITH id=:id', ['id' => 4]);
$result = $connection->fetchOne();

$connection->update('user', ['first_name' => 'john', 'last_name' => 'cool'], 4);

$connection->delete('user', 4);



declare(strict_types=1);

namespace App\Entities;

use DateTime;
use Spade\Database\BaseEntity;

class User extends BaseEntity
{
    private ?string $firstName;
    private ?string $lastName;
    private ?string $email;
    private ?DateTime $birthDate;
    
    ...

class UserRepository extends BaseRepository
{
    public function getEntity(): string
    {
        return User::class;
    }

    public function find(int $id): ?User
    {
        /** @phpstan-ignore return.type */
        return parent::find($id);
    }
}

$templateService = new TemplateService(
    PROJECT_ROOT . '/src/Templates/Pages/',
    PROJECT_ROOT . '/src/Templates/Partials/'
);

return new ViewResponse('user/info', $data)

$data = [
    'user' => [
        'id' => $user->getId(),
        'firstName' => $user->getFirstName(),
        'lastName' => $user->getLastName(),
        'email' => $user->getEmail(),
        'birthday' => $user->getBirthDate()?->format('d.m.Y'),
    ]
];