PHP code example of 4asuite / nano-php

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

    

4asuite / nano-php example snippets


return [
    'GET' => [
        '/'             => [App\Controllers\HomeController::class, 'index'],
        '/users/(:num:id)' => [App\Controllers\UserController::class, 'show'],
        '/posts/(:any:slug)' => [App\Controllers\PostController::class, 'show'],
    ],
    'POST' => [
        '/users' => [App\Controllers\UserController::class, 'store'],
    ],
];


namespace App\Controllers;

use App\Core\Controller;
use App\Core\Response;

final class UserController extends Controller
{
    public function show(string $id): Response
    {
        return $this->view('users/show', ['id' => $id]);
    }

    public function store(): Response
    {
        $name = $this->request->post('name', '');
        // ...
        return $this->redirect('/users');
    }

    public function api(): Response
    {
        return $this->json(['status' => 'ok']);
    }
}

// Controller
return $this->view('users/show', ['name' => 'Alice'], 'main');

<!-- App/views/users/show.php -->
<h1><?= e($name) 

<!-- App/views/layouts/main.php -->
<!DOCTYPE html>
<html lang="<?= e(APP_DEFAULT_LANG) 

use App\Core\Session;

Session::set('user_id', 42);
$id  = Session::get('user_id');
$has = Session::has('user_id');
Session::remove('user_id');
Session::destroy();   // clears cookie + regenerates ID

use App\Core\Csrf;

// In a view form:
<?= Csrf::input() 

use App\Core\Lang;

Lang::load(['GREETING' => 'Hello, %s!']);

echo Lang::get('greeting', 'Alice');  // Hello, Alice!

use App\Core\HttpException;

throw new HttpException(403, 'Access denied.');
throw new HttpException(404, 'Not found.');
bash
composer create-project 4asuite/nano-php myproject
cd myproject