PHP code example of mapathe / core

1. Go to this page and download the library: Download mapathe/core 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/ */

    

mapathe / core example snippets


$container = Container::getInstance();

// Lier une classe
$container->bind(Interface::class, Implementation::class);

// Singleton
$container->singleton(Service::class);

// Résoudre
$service = $container->resolve(Service::class);

// Initialiser l'application
App::initialize();

// Récupérer un service
$controller = App::get('CitoyenController');
$service = App::get(\App\Service\CitoyenService::class);

// Dans routes/route.web.php
return [
    [
        'method' => 'GET',
        'path' => '/users/{id}',
        'controller' => 'CitoyenController',
        'action' => 'show',
        'middlewares' => ['auth']
    ],
    [
        'method' => 'POST',
        'path' => '/login',
        'action' => function() {
            echo "Connexion";
        }
    ]
];

// Résoudre la route courante
Router::resolve();

class CitoyenController extends AbstractController
{
    public function index() {
        $data = ['users' => []];
        $this->renderJson($data, "success", 200, "Liste récupérée");
    }
}

class User extends AbstractEntity
{
    public static function toObject(array $data): static {
        // Conversion array vers objet
    }
    
    public function toArray(): array {
        // Conversion objet vers array
    }
}

class UserRepository extends AbstractRepository
{
    public function findAll() {
        $stmt = $this->pdo->query("SELECT * FROM users");
        return $stmt->fetchAll();
    }
}

$session = Session::getInstance();

$session->set('user', $userData);
$user = Session::get('user');
Session::

// Structure du projet
/src
  /core           # Classes du framework
  /config
    services.yml  # Configuration des services
/routes
  route.web.php   # Définition des routes

// Bootstrap