1. Go to this page and download the library: Download dynart/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/ */
dynart / micro example snippets
use Dynart\Micro\Micro;
use Dynart\Micro\WebApp;
namespace App\Controllers;
use Dynart\Micro\Attribute\Route;
class HomeController {
#[Route('GET', '/')]
public function index(): string {
return '<h1>Hello world!</h1>';
}
}
// Register
Micro::add(ConfigInterface::class, Config::class);
Micro::add(MyService::class);
// Resolve (auto-creates with constructor injection)
$service = Micro::get(MyService::class);
// Factory (new instance each time)
$instance = Micro::create(MyService::class, ['extraParam']);
$router->add('/users/?/posts/?', [UserController::class, 'posts']);
$router->add('/api/login', [AuthController::class, 'login'], 'POST');
$router->add('/search', [SearchController::class, 'index'], 'BOTH'); // GET + POST
class BookController {
#[Route('GET', '/books')]
public function list(): string { /* ... */ }
#[Route('GET', '/books/?')]
public function show(string $id): array { /* ... */ }
}
class MyCliApp extends CliApp {
public function init(): void {
$commands = Micro::get(CliCommandsInterface::class);
$commands->add('migrate', [MigrateCommand::class, 'run']);
$commands->add('seed', [SeedCommand::class, 'run']);
}
}
// Usage: php cli.php migrate -step 5 --fresh
class App extends WebApp {
public function init(): void {
parent::init();
$this->useJwtAuth();
// Map sub → permissions (your DB lookup)
Micro::get(JwtAuthInterface::class)->setUserResolver(
function(string $sub, object $payload): JwtUserInterface {
$perms = Micro::get(UserService::class)->getPermissionsForSub($sub);
return new JwtUser($sub, $perms);
}
);
}
}
#[Authorize] // class-level: all methods n/stats')]
public function stats(): array { /* inherits class-level auth */ }
#[Route('GET', '/api/admin/ping')]
#[AllowAnonymous] // overrides class-level
public function ping(): array { return ['ok' => true]; }
}
class ApiController {
#[Route('GET', '/api/books')]
#[Authorize] // any authenticated user
public function list(): array { /* ... */ }
#[Route('DELETE', '/api/books/?')]
#[Authorize('admin')] //
class AuthMiddleware implements MiddlewareInterface {
public function run(): void {
// check auth, redirect if needed
}
}
// In AbstractApp::init():
$this->addMiddleware(AuthMiddleware::class);
ini
; translations/en.ini
greeting = Hello, {name}!
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.