PHP code example of webpagestudio / wps-micro

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

    

webpagestudio / wps-micro example snippets




declare(strict_types=1);

use WpsMicro\Core\Env;
use WpsMicro\Core\Kernel;

$rootPath = dirname(__DIR__);


/** @var \WpsMicro\Core\Kernel $kernel */
$kernel = 

use WpsMicro\Core\Config;
use WpsMicro\Core\Container;
use WpsMicro\Core\Kernel;
use WpsMicro\Core\Router;

$container = new Container();
$container->instance(Router::class, new Router());

$kernel = new Kernel(new Config($config), $container);

use App\Controllers\ControllerProduct;
use WpsMicro\Core\Router;

return static function (Router $router): void {
    $router->get('/products/{id}', [ControllerProduct::class, 'actionShow'])
        ->whereNumber('id')
        ->name('products.show');
    $router->post('/cart/add', [ControllerCart::class, 'actionAdd']);
    $router->delete('/cart/{id}', [ControllerCart::class, 'actionRemove']);
};

$router->get('/posts/{slug}', [ControllerPost::class, 'actionShow'])
    ->where('slug', '[a-z0-9-]+')
    ->name('posts.show');

$router->get('/orders/{order}', [ControllerOrder::class, 'actionShow'])
    ->whereUuid('order')
    ->name('orders.show');

$router->group([
    'prefix' => '/admin',
    'name' => 'admin.',
    'middleware' => AuthMiddleware::class,
], static function (Router $router): void {
    $router->get('/products', [ControllerProduct::class, 'actionIndex'])
        ->name('products.index');

    $router->post('/products', [ControllerProduct::class, 'actionStore'])
        ->middleware(CsrfMiddleware::class)
        ->name('products.store');
});

$path = $router->url('products.show', ['id' => 42], ['tab' => 'details']);
// /products/42?tab=details

namespace App\Controllers;

use WpsMicro\Core\Controller;
use WpsMicro\Core\Response;

final class ControllerProduct extends Controller
{
    public function actionShow(string $id): Response
    {
        return $this->render('products/show.twig', ['id' => $id]);
    }
}

return $this->render('products/index.twig', ['products' => $products]);
return $this->json(['products' => $products]);
return $this->redirect('/products');

$response
    ->setHeader('Cache-Control', 'no-store')
    ->addHeader('Set-Cookie', 'theme=dark; Path=/; SameSite=Lax')
    ->addHeader('Set-Cookie', 'locale=en; Path=/; SameSite=Lax');

use WpsMicro\Core\Middleware;
use WpsMicro\Core\Request;
use WpsMicro\Core\Response;

final class AuthMiddleware implements Middleware
{
    public function handle(Request $request, callable $next): Response
    {
        return $next($request);
    }
}

$validated = $this->validate([
    'quantity' => 'title' => '

$validator->addRule(
    'divisible_by',
    static function (mixed $value, string $field, array $data, ?string $parameter): bool|string {
        $divisor = (int) $parameter;

        return $divisor > 0 && (int) $value % $divisor === 0
            ?: $field . ' must be divisible by ' . $parameter . '.';
    },
);

$validated = $validator->validate($input, [
    'quantity' => '

use WpsMicro\Core\Migration;

return new class extends Migration {
    public function up(PDO $db): void
    {
        $db->exec('CREATE TABLE products (id INTEGER PRIMARY KEY)');
    }

    public function down(PDO $db): void
    {
        $db->exec('DROP TABLE products');
    }
};

use WpsMicro\Core\Console\Commands\RouteListCommand;
use WpsMicro\Core\Router;

/** @var Router $router */
$router = $kernel->getContainer()->get(Router::class);

$console
    ->add(new RouteListCommand($router))
    ->add(new MakeControllerCommand($root . '/app/Controllers', 'App\\Controllers'))
    ->add(new MakeModelCommand($root . '/app/Models', 'App\\Models'))
    ->add(new MakeMigrationCommand($root . '/database/migrations'));