PHP code example of robertwesner / simple-mvc-php

1. Go to this page and download the library: Download robertwesner/simple-mvc-php 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/ */

    

robertwesner / simple-mvc-php example snippets


Route::post('/api/login', function (Request $request) {
    // Reads either Query or JSON-Body Parameter
    $password = $request->getRequestParameter('password');
    if ($password === null) {
        return Route::response('Bad Request', 400);
    }

    // ...
    
    return Route::json([
        'success' => $success,
    ]);
});

Route::post('/api/logout', function () {
    // ...
});

// Also able to read URI parameters
Route::get('/api/users/(?<userId>\d+)', function (Request $request) {
    $userId = $request->getUriParameter('userId'); // Returns numeric userId from capture group

    // ...
});

// 404 page, FALLBACK will be called when no other route matches
Route::get(Route::FALLBACK, function (Request $request) {
    return Route::render('404.twig');
});

Route::get('/', function () {
    // ...

    return Route::render('main.twig', [
        'loggedIn' => $loggedIn,
    ]);
});

final class UserService
{
    // ...
}

readonly class UserController
{
    public function __construct(
        private UserService $userService,
    ) {}

    public function all(): ResponseInterface
    {
        // ...
    }

    public function get(Request $request): ResponseInterface
    {
        // ...
    }

    public function create(Request $request): ResponseInterface
    {
        // ...
    }

    public function delete(Request $request): ResponseInterface
    {
        // ...
    }
}

// Note: this api/users', [UserController::class, 'all']);
Route::get('/api/users/(?<userId>\d+)', [UserController::class, 'get']);
Route::post('/api/users', [UserController::class, 'create']);
Route::delete('/api/users/(?<userId>\d+)', [UserController::class, 'delete']);

// Autowired service class (AuthenticationService) inside Route
// Note: this ationService $authenticationService) {
    // ...
});

Configuration::CONTAINER
    // Either let the container do all the heavy lifting via class names,
    // MySQLEntityManager would be automatically instantiated by the container.
    // This is necessary for usage of interfaces, rather than implementations.
    ::instantiate(EntityManagerInterface::class, MySQLEntityManager::class)
    // Or pass your own instance when necessary, since Bar is not to be autowired.
    ::register(FooInterface::class, new Bar('SOME VALUE'));

Configuration::BUNDLES
    ::load(FooBundle::class)
    // Optionally with additional configuration of any type, depending on the bundle.
    ::load(BarBundle::class, ['faz' => 'baz']);

Configuration::CONTAINER
    ::instantiate(ThrowableHandlerInterface::class, PrintThrowableHandler::class);

Configuration::CONTAINER
    ::instantiate(ThrowableHandlerInterface::class, StderrThrowableHandler::class);
bash
composer create-project robertwesner/simple-mvc-php-docker-template
bash
composer create-project robertwesner/simple-mvc-php-template
bash
composer