### Create a Minimal HTTP Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/index.md This example demonstrates how to set up a basic HTTP application using FastApplication, define a GET route for the homepage, and run the application. Ensure you have the Composer autoloader included. ```php listen('GET', '/', function (ServerRequestInterface $request): string { return 'Hello, Piko!'; }); $app->run(); ``` -------------------------------- ### Module Bootstrap Method Example Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/modules.md Implement the bootstrap() method within a module to perform custom initialization logic during application startup. This example sets the application language. ```php getApplication()->language = 'fr'; } } ``` -------------------------------- ### Install Piko User Component Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/user.md Install the Piko User component using Composer. ```bash composer require piko/user ``` -------------------------------- ### Install DbRecord Component Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Use Composer to install the DbRecord component. This command adds the necessary package to your project. ```bash composer require piko/db-record ``` -------------------------------- ### User Login and Logout Controller Example Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/user.md Demonstrates a typical controller implementation for handling user login and logout actions. It shows how to use the User component for authentication and redirection. ```php request->getMethod() === 'POST') { $post = $this->request->getParsedBody(); $identity = UserIdentity::findByUsername($post['username'] ?? ''); if ($identity instanceof UserIdentity && $identity->validatePassword($post['password'] ?? '') ) { $this->user->login($identity); return $this->redirect($this->getUrl('site/default/index')); } $error = 'Authentication failed'; } return $this->render('login', [ 'error' => $error, ]); } public function logoutAction() { $this->user->logout(); return $this->redirect($this->getUrl('site/default/index')); } } ``` -------------------------------- ### Standalone PHP Application Setup Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/i18n.md Demonstrates how to use the Piko/i18n package in a standalone PHP application. This includes setting up the messages directory, creating an I18n instance, and optionally registering the helper function. ```php directory containing .php files 'app' => __DIR__ . '/messages', ], 'fr'); // default language is "fr" // Optional: register this instance for the __() helper I18n::setInstance($i18n); // Direct usage echo $i18n->translate('app', 'Hello {name}', ['name' => 'John']) . "\n"; // Outputs: Bonjour John // Using the helper echo __('app', 'Translation test') . "\n"; // Outputs: Test de traduction ``` -------------------------------- ### Install Piko Framework via Composer Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/index.md Use this command to add the Piko framework to your project dependencies. ```bash composer require piko/framework ``` -------------------------------- ### Install Piko/i18n Package Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/i18n.md Install the Piko/i18n component using Composer. This package provides the core functionality for internationalization. ```bash composer require piko/i18n ``` -------------------------------- ### Example Piko Layout Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/views.md A sample layout file demonstrating how to include charset, title, head assets, stylesheets, and body content. It utilizes the global `Piko` helper class for aliasing web assets. ```php <?= $this->escape($this->title) ?> head() ?> endBody() ?> ``` -------------------------------- ### User Component Configuration for Access Control Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/user.md Shows a configuration example for the User component, specifically setting the `checkAccess` callback. This callback is used by `User::can()` to determine permissions. ```php return [ // ... 'components' => [ 'Piko\User' => [ 'identityClass' => 'app\modules\site\models\UserIdentity', 'checkAccess' => 'app\modules\site\models\UserIdentity::checkAccess', ], // ... ], ]; ``` -------------------------------- ### Run Piko Application Locally Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/getting-started.md Start the PHP built-in web server from your project's root directory, setting the 'web/' directory as the document root. Access your application via http://localhost:8080/. ```bash cd yourprojectname php -S localhost:8080 -t web ``` -------------------------------- ### Create New Piko Project Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/getting-started.md Use Composer to create a new project from the official Piko project skeleton. This sets up the basic directory structure and installs necessary dependencies. ```bash composer create-project piko/project yourprojectname ``` -------------------------------- ### Example Translation File (fr.php) Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/i18n.md A sample translation file for French. It maps source strings (keys) to their translated equivalents (values). Placeholders like {name} can be used for dynamic content. ```php 'Test de traduction', 'Hello {name}' => 'Bonjour {name}', ]; ``` -------------------------------- ### Registering Route Handlers with Multiple Methods Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/fast-application.md Shows how to register a single route handler for multiple HTTP methods (GET and POST) and how to explicitly receive route parameters. Uses FastApplication::createResponse for creating PSR-7 responses. ```php use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; $app->listen(['GET', 'POST'], '/article/:id', function (ServerRequestInterface $request, array $params): ResponseInterface { $id = (int) $params['id']; // ... load the article ... return FastApplication::createResponse("Article #$id"); } ); ``` -------------------------------- ### Modular Application Configuration Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/index.md An example configuration array for a Piko modular application. This array defines application-wide settings such as base path, default layout, error route, language, components, modules, and bootstrap modules. ```php __DIR__, 'defaultLayoutPath' => '@app/layouts', 'defaultLayout' => 'main', 'errorRoute' => 'site/default/error', 'language' => 'fr', 'components' => [ // ... ], 'modules' => [ // ... ], 'bootstrap' => [ // ... ], ]; ``` -------------------------------- ### Controller Action with Path Parameter Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/routing.md Example of a controller action method that accepts a path parameter. The parameter 'id' is automatically passed to the method. ```php namespace app\modules\user\controllers; use Piko\Controller; class DefaultController extends Controller { public function viewAction(string $id): string { return "User #$id"; } } ``` -------------------------------- ### Get Router Instance Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/FastApplication.md Retrieves the router instance associated with the FastApplication. This can be used to access and manipulate routing configurations. ```php public getRouter(): Piko\Router ``` -------------------------------- ### Get View Component Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Retrieves the application's View component instance. ```php protected getView(): Piko\View\ViewInterface|null ``` -------------------------------- ### Retrieve User Component Instance Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/user.md Get an instance of the User component from the application container. ```php /** @var Piko\User $user */ $user = $app->getComponent('Piko\User'); ``` -------------------------------- ### Get a Module Instance Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Retrieve an instance of a specific module by its identifier. This method may throw a RuntimeException if the module is not found. ```php public getModule(string $moduleId): Piko Module ``` -------------------------------- ### Get Method Arguments Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Analyzes a method to determine its arguments, optionally binding data to them. ```php private getMethodArguments(string $methodName, array $data = []): array ``` -------------------------------- ### Resolving and Setting Aliases in Piko Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/concepts.md Demonstrates how to use Piko::getAlias() to resolve built-in and custom aliases, and Piko::setAlias() to define new ones. If the string does not start with '@', it is returned unchanged. Unknown aliases return false. ```php echo Piko::getAlias('@app/modules/site'); // /usr/local/share/myapp/modules/site echo Piko::getAlias('@webroot/documents'); // /var/www/documents echo Piko::getAlias('@web/css/styles.css'); // /css/styles.css // Custom alias Piko::setAlias('@lib', '/usr/local/share/lib'); echo Piko::getAlias('@lib/pdf/manual.pdf'); // /usr/local/share/lib/pdf/manual.pdf ``` -------------------------------- ### Basic FastApplication Usage Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/fast-application.md Demonstrates setting up a FastApplication, registering simple and parameterized routes, and running the application. Ensure you have the autoloader included. ```php use Piko\FastApplication; use Psr\Http\Message\ServerRequestInterface; require 'vendor/autoload.php'; $app = new FastApplication(); // Simple route $app->listen('GET', '/', function (ServerRequestInterface $request) { return 'App home'; }); // Route with a parameter $app->listen('GET', '/user/:name', function (ServerRequestInterface $request) { $name = $request->getAttribute('name'); return "Hello $name"; }); $app->run(); ``` -------------------------------- ### Application Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Application.md Initializes a new instance of the Application class with optional configuration. ```APIDOC ## __construct ### Description Constructor ### Parameters #### Parameters - **$config** (array>): The application configuration. ### Code ```php public __construct(array> $config = []) : void ``` ``` -------------------------------- ### Initialize Router with Configuration Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Router.md Constructor for the Router class. Accepts a configuration array to set public properties and define initial routes. ```php $router = new Router([ 'baseUri' => '/subdir', 'routes' => [ '/' => 'home', '/user/:id' => 'userView', '/:module/:controller/:action' => ':module/:controller/:action', ] ]); ``` -------------------------------- ### getModule Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Gets a sub-module of this module by its ID. ```APIDOC ## getModule ### Description Get a sub module of this module. ### Method public ### Parameters #### Parameters - **$moduleId** (string) - The module identifier. ### Throws - \RuntimeException - If module not found. ### Return: - **\Piko\Module** ``` -------------------------------- ### __construct() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/View.md Initializes the View component with optional configuration. ```APIDOC ## __construct() ### Description Constructs the View object, accepting an optional configuration array. ### Method __construct ### Parameters - **$config** (array) - Optional - Configuration array for the View component. ### Return: - mixed ``` -------------------------------- ### User Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/User.md Initializes a new User instance, optionally with configuration parameters. ```APIDOC ## __construct() ### Description Constructor for the User class. ### Method public ### Parameters - **$config** (array) - Optional - Configuration array for the User instance. ### Return: - mixed ``` -------------------------------- ### Get PDO Connection in Piko Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Retrieve the PDO database connection component within a Piko application context. ```php /** @var Piko\Application $app */ $db = $app->getComponent(PDO::class); ``` -------------------------------- ### Get Handler Routes Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Router.md Protected method to retrieve all routes associated with a specific handler. Useful for internal route management. ```php protected gethandlerRoutes(string $handler): array ``` -------------------------------- ### Instantiate ModularApplication Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md The constructor for ModularApplication accepts an optional configuration array to set up the application, including module definitions. ```php public __construct(array> $config = []): void ``` -------------------------------- ### Create Hello World Controller Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/getting-started.md Define a controller that extends Piko\Controller and includes an action method (e.g., worldAction). The namespace must match the PSR-4 configuration. ```php Hello world!'; } } ``` -------------------------------- ### Run the Modular Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Execute the application's main logic. This method is inherited and responsible for bootstrapping and handling requests. ```php public run(): void ``` -------------------------------- ### Retrieving User Component Explicitly Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/user.md Shows how to get the User component from the application if constructor injection is not used. This is an alternative way to access the component. ```php $app = $this->module->getApplication(); /** @var Piko\User $user */ $user = $app->getComponent('Piko\User'); $user->login($identity); ``` -------------------------------- ### Router Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Router.md Initializes the Router with optional configuration settings, including base URI and route definitions. ```APIDOC ## __construct() ### Description Constructor ### Parameters - **$config** (array) - Optional: A configuration array to set public properties and routes. ### Example ```php $router = new Router([ 'baseUri' => '/subdir', 'routes' => [ '/' => 'home', '/user/:id' => 'userView', '/:module/:controller/:action' => ':module/:controller/:action', ] ]); ``` ``` -------------------------------- ### Register Path Alias Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Piko.md Registers a short name (alias) to represent a longer path. The alias must start with '@'. An InvalidArgumentException is thrown if the path is invalid. ```php public static setAlias(string $alias, string $path): void ``` -------------------------------- ### Checking User Authentication Status Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/user.md Provides an example of how to check if a user is authenticated and retrieve their identity and ID. This is useful for conditional logic based on login status. ```php /** @var Piko\User $user */ $user = $app->getComponent('Piko\User'); if (!$user->isGuest()) { $identity = $user->getIdentity(); // instance of UserIdentity $id = $user->getId(); // string|int var_dump($identity); var_dump($id); } ``` -------------------------------- ### run Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Executes the application's main logic. ```APIDOC ## run() ### Description Run the application. ### Method public ### Return: void ``` -------------------------------- ### Listen to beforeSave Event Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Register an event listener to customize save operations. The listener can invalidate the event to cancel the save, for example, if a required field is not set. ```php use Piko\DbRecord\Event\BeforeSaveEvent; $contact = new Contact($db); $contact->on(BeforeSaveEvent::class, function (BeforeSaveEvent $event): void { if (!$event->record->name) { // Invalidate the event, which cancels the save $event->isValid = false; } }); ``` -------------------------------- ### __construct Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/InitEvent.md Initializes the InitEvent with the Piko application instance. ```APIDOC ## __construct ### Description Constructor for the InitEvent class. ### Method __construct ### Parameters #### Parameters - **$app** (`Piko\Application`) - Description not provided. ``` -------------------------------- ### Getting the I18n Singleton Instance Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/I18n.md Retrieves the singleton instance of the I18n class. This allows access to translation functionalities without needing to instantiate the class directly. ```php public static getInstance(): null|\Piko\I18n ``` -------------------------------- ### Basic Controller with Hello Action Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/controllers.md Defines a controller that extends Piko\Controller and includes a simple action method returning a string. ```php namespace app\modules\site\controllers; class DefaultController extends \Piko\Controller { /** * Route: site/default/hello */ public function helloAction() { return "Hello world!"; } } ``` -------------------------------- ### login() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/User.md Initiates a user session and sets the user's identity. ```APIDOC ## login() ### Description Start the session and set user identity. ### Method public ### Parameters - **$identity** (\Piko\User\IdentityInterface) - The user identity object to set. ### Return: - void ``` -------------------------------- ### ModularApplication Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Initializes a new instance of the ModularApplication class with optional configuration. ```APIDOC ## __construct() ### Description Constructor for the ModularApplication class. ### Method public ### Parameters - **$config** (array) - Optional - The application configuration. ### Return: void ``` -------------------------------- ### Configure PDO Connection in Piko Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Example configuration for a MySQL PDO connection within a Piko application. Ensure a PDO component is registered in your application's configuration. ```php return [ // ... 'components' => [ PDO::class => [ 'construct' => [ 'mysql:dbname=' . getenv('MYSQL_DB') . ';host=' . getenv('MYSQL_HOST'), getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'), ], ], ], ]; ``` -------------------------------- ### Get View Path Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Returns the directory containing view files for this controller. The default implementation uses a directory named after the controller ID within the module's viewPath. ```php protected getViewPath(): string ``` -------------------------------- ### Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/I18n.md Initializes the I18n class with translations and a language code. ```APIDOC ## __construct ### Description Constructor The $translations argument should contains a key-value paired array of domain / path. Example : ```php [ 'app' => '@app/messages' ] ``` ### Parameters **$translations** (default: []): **$language** (default: 'en'): The language code ### Return: **mixed** ``` -------------------------------- ### create() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Creates an object and resolves constructor dependencies from application components. ```APIDOC ## create() ### Description Creates an object and resolves constructor dependencies from application components. ### Method protected ### Parameters - **$class** (class-string) - Description not provided - **$overrides** (array) - Optional - Constructor argument overrides indexed by parameter name. ### Return: - **object** ``` -------------------------------- ### Define Contact Model with Different Column Name Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Map a model property to a different database column name using the 'name' option in the Column attribute. This example maps the 'name' property to the 'full_name' column. ```php use PDO; use Piko\DbRecord; use Piko\DbRecord\Attribute\Table; use Piko\DbRecord\Attribute\Column; #[Table(name: 'contact')] class Contact extends DbRecord { #[Column(primaryKey: true)] public ?int $id = null; #[Column(name: 'full_name')] public string $name; } ``` -------------------------------- ### View Class Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/View.md Initializes the View class with an optional configuration array. ```php public function __construct(array<$string,mixed> $config = []): mixed ``` -------------------------------- ### Entry Script for Modular Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/index.md The main entry point for a modular Piko application, typically located in public/index.php. It initializes the application with configuration and runs it. ```php run(); ``` -------------------------------- ### run() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Executes the application, processing the incoming request and emitting headers if enabled. ```APIDOC ## run() ### Description Run the application. ### Method public ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **$request** (Psr\Http\Message\ServerRequestInterface | null) - Optional. The server request object. Defaults to null. - **$emitHeaders** (bool) - Optional. Controls whether headers will be emitted (header() function called). Defaults to true. ### Request Example ```php // Example usage: $app->run(); $app->run($request); $app->run($request, false); ``` ### Response #### Success Response (void) This method does not return a value. #### Response Example None ``` -------------------------------- ### Basic Model Usage with Piko\ModelTrait Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/models.md Demonstrates how to define a model using Piko\ModelTrait, declare public properties as attributes, and use the bind() method for mass assignment and toArray() for exporting data. Unknown keys during binding are ignored. ```php bind([ 'firstName' => 'John', 'lastName' => 'Lennon', 'role' => 'admin', // ignored because there is no matching public property ]); $data = $form->toArray(); // [ // 'firstName' => 'John', // 'lastName' => 'Lennon', // ] ``` -------------------------------- ### Run Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/FastApplication.md Executes the application, processing the incoming request and emitting the response. It can optionally control header emission. ```php public run( Psr\Http\Message\ServerRequestInterface $request = null, bool $emitHeaders = true ): void ``` -------------------------------- ### Configuring Piko Components Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/concepts.md Shows how to configure application-wide singletons (components) in Piko's configuration file. Components can be defined via public properties, constructor arguments, custom identifiers, or as factory callables for lazy loading. ```php use Piko\View; use Monolog\Logger; use Monolog\Handler\StreamHandler; return [ // ... 'components' => [ // 1. View component configured via public properties View::class => [ // Default charset is UTF-8 'charset' => 'ISO-8859-1', ], // 2. Logger component created lazily via a factory Logger::class => function (): Logger { $logger = new Logger('app'); $logger->pushHandler( new StreamHandler(__DIR__ . '/../var/log/app.log', Logger::DEBUG) ); return $logger; }, // 3. PDO configured using constructor arguments (lazy-loaded) PDO::class => [ 'construct' => [ 'mysql:dbname=' . getenv('MYSQL_DB') . ';host=' . getenv('MYSQL_HOST'), getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'), ], ], // 4. Alternative style: custom id with an explicit class 'mailer' => [ 'class' => Nette\Mail\SmtpMailer::class, 'construct' => [ getenv('SMTP_HOST'), getenv('SMTP_USER'), getenv('SMTP_PASSWORD'), (int) getenv('SMTP_PORT'), getenv('SMTP_ENCRYPTION'), ], ], ], ]; ``` -------------------------------- ### Bootstrapping Modules in Application Configuration Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/modules.md Enable modules to participate in the application bootstrap process by listing them in the application's 'bootstrap' array. The module's bootstrap() method will be called if it exists. ```php [ 'site' => 'app\\modules\\site\\Module', ], 'bootstrap' => [ 'site', ], ]; ``` -------------------------------- ### Controller with Dependency Injection Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/controllers.md Demonstrates dependency injection in a controller's constructor, resolving arguments from the application container. ```php namespace app\modules\products\controllers; use PDO; use Piko\User; class ProduitsController extends \Piko\Controller { public function __construct(private User $user, private PDO $db) { } } ``` -------------------------------- ### Handle HTTP Request Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md The main entry point for handling an incoming HTTP request and returning a response. ```php public handle( Psr\Http\Message\ServerRequestInterface $request ): Psr\Http\Message\ResponseInterface ``` -------------------------------- ### Create a View File Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/getting-started.md Define the HTML content for a view. This file should be placed in the controller's view directory. ```php

Hello world!

``` -------------------------------- ### DbRecord Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/DbRecord.md Initializes a new DbRecord instance with a PDO database connection. ```APIDOC ## __construct ### Description Constructor for the DbRecord class. ### Method public ### Parameters #### Parameters - **$db** (PDO) - Required - A PDO instance to connect to the database. ### Return: - **mixed** ``` -------------------------------- ### getView() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Returns the application View component. ```APIDOC ## getView() ### Description Returns the application View component. ### Method protected ### Return: - **Piko\View\ViewInterface|null** ``` -------------------------------- ### setApplication() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Sets the application instance for the module. ```APIDOC ## public setApplication() ### Description Sets the application instance for the module. ### Method public ### Signature `setApplication( Piko\ModularApplication $app ): void` ### Parameters * **$app** (`Piko\ModularApplication`) - The application instance to set. ``` -------------------------------- ### Controller Forward and Redirect Actions Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/controllers.md Demonstrates using `redirect()` to send an external HTTP redirect and `forward()` to dispatch another route internally. ```php public function saveAction() { return $this->redirect($this->getUrl('site/default/user', ['username' => 'Bill'])); } public function validateAction() { return $this->forward('site/default/user', ['username' => 'Bill']); } ``` -------------------------------- ### Run Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Application.md Executes the application, processing requests and emitting responses. ```APIDOC ## run ### Description Run the application. ### Parameters #### Parameters - **$request** (\Psr\Http\Message\ServerRequestInterface|null): The server request to handle. If null, the application may attempt to resolve it. - **$emitHeaders** (bool): Controls whether headers will be emitted (header() function called). Defaults to true. ### Code ```php public run(\Psr\Http\Message\ServerRequestInterface|null $request = null, bool $emitHeaders = true): void ``` ``` -------------------------------- ### Controller for Loading DbRecord Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/models.md Use the controller helper `create` to instantiate a `DbRecord` model with its dependencies resolved. Load a record by its primary key using the `load` method. ```php create(Contact::class); $contact->load($id); return $this->render('display', [ 'contact' => $contact, ]); } } ``` -------------------------------- ### getApplication Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Retrieves the application instance associated with this module. ```APIDOC ## getApplication ### Description Retrieves the application instance associated with this module. ### Method public ### Return: - **\Piko\ModularApplication** ``` -------------------------------- ### Create Controller Instance - Piko Module Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Creates and returns a controller instance based on the provided controller ID. This is a protected method, intended for internal use within the module or its subclasses. ```php protected createController( string $controllerId ): Piko\Controller ``` -------------------------------- ### Constructor for Piko\I18n Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/I18n.md Initializes the I18n class with translations and a language code. The translations should be an array mapping domains to their message file paths. ```php public __construct(array $translations = [], string $language = 'en'): mixed ``` -------------------------------- ### Create and Save a New Entity Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Instantiate a DbRecord model, set its properties, and save it to the database. The primary key will be generated upon insertion. ```php $contact = new Contact($db); $contact->name = 'Joe'; $contact->order = 1; $contact->save(); echo $contact->id; // Generated ID for Joe ``` -------------------------------- ### listen Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/FastApplication.md Registers a route handler for specific HTTP request methods and paths. ```APIDOC ## listen ### Description Register a route handler. ### Method public listen(string|string[] $requestMethod, string $path, callable $handler): void ### Parameters - **$requestMethod** (string|string[]) - Required - The allowed request(s) method(s) - **$path** (string) - Required - The route path - **$handler** (callable) - Required - A callable handler, which have the following signature: ``` function(Psr\Http\Message\ServerRequestInterface $request): string|Psr\Http\Message\ResponseInterface ``` ``` -------------------------------- ### Configure Piko Modules Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Define module configurations using an array. Modules can be specified by their class name or with additional configuration like 'class' and 'layoutPath'. ```php [ 'moduleId' => 'moduleClassName' ] ``` ```php [ 'moduleId' => [ 'class' => 'moduleClassName', 'layoutPath' => '/some/path' // ... ] ] ``` -------------------------------- ### Instantiate DbRecord Model in Controller Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Use the `create()` factory method within a Piko Controller to instantiate DbRecord models, leveraging automatic dependency injection for constructor arguments. ```php use PDO; use Piko\Controller; use Psr\Http\Message\ResponseInterface; use App\Model\Contact; // Adjust the namespace to your application class ContactController extends Controller { public function createAction(): ResponseInterface { /** @var Contact $contact */ $contact = $this->create(Contact::class); $contact->name = 'Joe'; $contact->order = 1; $contact->save(); return $this->redirect($this->getUrl('contact/index')); } } ``` -------------------------------- ### HttpException Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/HttpException.md Use the constructor to set the HTTP status code and message for the exception. The code should be a valid HTTP status code, defaulting to 404. ```php public __construct(int $code = 404, string $message = '', hrowable $previous = null): mixed ``` -------------------------------- ### render Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ViewInterface.md Renders a specified view file with provided data. ```APIDOC ## render ### Description Render the view. ### Method public ### Signature render(string $file, array $model = []): string ### Parameters #### Parameters - **$file** (string) - Required - The view file name. - **$model** (array) - Optional - An array of data (name-value pairs) to transmit to the view. ### Return: **string** - The view's output. ``` -------------------------------- ### __construct Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/HttpException.md The constructor for HttpException. It initializes the exception with an HTTP status code and message, and optionally a previous exception. ```APIDOC ## __construct() ### Description Constructor set http header with response code and message if code is given. ### Method public ### Parameters #### Parameters - **$code** (int) - Optional - The exception code (should be an HTTP status code, eg. 404). Defaults to 404. - **$message** (string) - Optional - The exception message. Defaults to an empty string. - **$previous** (Throwable) - Optional - A previous exception. Defaults to null. ### Return: mixed ``` -------------------------------- ### Render View Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Renders a specified view file with optional data. The rendered output is returned as a string. ```php protected render(string $viewName, array $data = []): string ``` -------------------------------- ### Map Module Views to Multiple Theme Directories Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/views.md Define multiple theme directories for overriding module views. Directories are checked in order until a matching view file is found. ```php [ 'Piko\View' => [ 'themeMap' => [ '@app/modules/site/views' => [ '@app/themes/mychildtheme', '@app/themes/myparenttheme', ], ], ], ], ]; ``` -------------------------------- ### Action Parameters Mapping Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/controllers.md Shows how action method parameters are automatically mapped from route and query parameters, with basic scalar type conversion. ```php public function showAction(int $id, bool $preview = false) { // $id and $preview are mapped from request parameters } ``` -------------------------------- ### __construct Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/BeforeRenderEvent.md Constructor for the BeforeRenderEvent class. ```APIDOC ## __construct ### Description Constructor for the BeforeRenderEvent class. ### Method public ### Parameters #### Parameters - **$view** (\Piko\View) - Required - A view instance - **$file** (string) - Required - A view script path - **$model** (array) - Optional - The view script model ### Return: - mixed ``` -------------------------------- ### Configure Static Routes with Base URI Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/routing.md Define static routes and set a base URI if the application is deployed in a subdirectory. The base URI is removed from incoming paths and prepended when generating URLs. ```php [ 'Piko\Router' => [ 'construct' => [[ 'baseUri' => '/subdir', 'routes' => [ '/' => 'site/default/index', '/home' => 'site/default/index', ], ]], ], // ... ], ]; ``` -------------------------------- ### run Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/FastApplication.md Executes the application, processing incoming requests and sending responses. It can optionally control header emission. ```APIDOC ## run ### Description Run the application. ### Method public run( Psr\Http\Message\ServerRequestInterface $request = null, bool $emitHeaders = true ): void ### Parameters - **$request** (Psr\Http\Message\ServerRequestInterface) - Optional - The server request object. - **$emitHeaders** (bool) - Optional - Controls whether headers will be emmited (header() function called). ``` -------------------------------- ### Retrieving Piko Components Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/concepts.md Illustrates how to retrieve component instances from the Piko application container using Application::getComponent(). Constructor parameters with non-builtin type hints are resolved from this container, enabling dependency injection. ```php /** @var PDO $db */ $db = $app->getComponent(PDO::class); ``` -------------------------------- ### createObject Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Creates an object with constructor dependencies resolved from registered components. ```APIDOC ## createObject ### Description Create an object with constructor dependencies resolved from registered components. ### Method public ### Parameters #### Parameters - **$class** (class-string) - The class to instantiate. - **$overrides** (array) - Optional - Constructor argument overrides indexed by parameter name. ### Throws - \RuntimeException - If class is not found or mandatory dependency is not resolvable. ### Return: - **object** ``` -------------------------------- ### Configure View Extension Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/views.md Change the default file extension for view files from `.php` to `.phtml` in the `Piko\View` component configuration. ```php [ 'Piko\View' => [ 'extension' => 'phtml', ], ], ]; ``` -------------------------------- ### handle() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Handles an incoming HTTP request and returns a response. ```APIDOC ## handle() ### Description Handles an incoming HTTP request and returns a response. ### Method public ### Parameters - **$request** (Psr\Http\Message\ServerRequestInterface) - Description not provided ### Return: - **Psr\Http\Message\ResponseInterface** ``` -------------------------------- ### Create Object with Dependencies Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Creates an object, resolving constructor dependencies from application components. Constructor argument overrides can be provided. ```php protected create( class-string $class, array<string,mixed> $overrides = [] ): object ``` -------------------------------- ### __construct Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Column.md Constructor for the Column class. Initializes a new instance of the Column with optional primary key and name settings. ```APIDOC ## __construct() ### Description Constructor for the Column class. ### Method public ### Parameters #### Parameters - **$primaryKey** (bool) - Optional - Indicates if the field is a primary key. Default is false. - **$name** (string|null) - Optional - The name of the field. Default is null. ### Return: **mixed** ``` -------------------------------- ### View Script for User List Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/controllers.md A simple PHP view script that iterates over a 'users' array and displays first and last names. ```php
``` -------------------------------- ### Registering Modules in Application Configuration Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/modules.md Declare available modules in the application's configuration file. Modules can be registered by their class name or a configuration array. ```php [ 'site' => 'app\\modules\\site\\Module', 'blog' => 'app\\modules\\blog\\Module', 'admin' => 'app\\modules\\admin\\Module', 'api' => [ 'class' => 'app\\modules\\api\\Module', ], ], ]; ``` -------------------------------- ### InitEvent Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/InitEvent.md The constructor for the InitEvent class. It accepts the Piko\Application instance as a parameter. ```php public __construct( Piko Application $app): mixed ``` -------------------------------- ### render() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Renders a view with the provided data. ```APIDOC ## render() ### Description Render a view. ### Method protected ### Parameters - **$viewName** (string) - The view file name. - **$data** (array) - Optional - An array of data (name-value pairs) to transmit to the view. ### Return: - **string** ``` -------------------------------- ### getInstance Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/I18n.md Retrieves the singleton instance of the I18n class. ```APIDOC ## getInstance ### Description Return I18n singleton instance ### Return: **null|\Piko\I18n** ``` -------------------------------- ### publish() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/AssetBundle.md Publishes the assets defined in the bundle to the public path. This makes the CSS and JS files accessible via the web server. ```APIDOC ## publish() ### Description Publish assets into public path ### Method public ### Endpoint N/A (Method Call) ### Parameters None ### Request Example ```php $assetBundle->publish(); ``` ### Response #### Success Response (void) This method does not return a value. #### Response Example N/A ``` -------------------------------- ### CreateControllerEvent Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/CreateControllerEvent.md Initializes a new instance of the CreateControllerEvent class. Requires a Piko\Controller object. ```php public __construct( Piko\Controller $controller ): mixed ``` -------------------------------- ### __construct Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Constructor for the Module class. ```APIDOC ## __construct ### Description Constructor for the Module class. ### Method public ### Parameters #### Parameters - **$config** (array) - Optional - Configuration array for the module. ### Return: - **mixed** ``` -------------------------------- ### Run Piko Application Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Executes the Piko application. Optionally accepts a ServerRequestInterface and controls header emission. ```php public run(PsrHttpMessageServerRequestInterface $request = null, bool $emitHeaders = true): void ``` -------------------------------- ### Accessing Route Parameters Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/fast-application.md Illustrates how to define a route with multiple parameters and access them using `$request->getAttribute()`. Parameters are extracted from the URL path. ```php $app->listen('GET', '/blog/:year/:slug', function (ServerRequestInterface $request) { $year = (int) $request->getAttribute('year'); $slug = $request->getAttribute('slug'); return "Post $slug from $year"; }); ``` -------------------------------- ### handle() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Handles an incoming server request and returns a response. This method is part of the RequestHandlerInterface. ```APIDOC ## public handle() ### Description Handles an incoming server request and returns a response. ### Method public ### Signature `handle( Psr\Http\Message\ServerRequestInterface $request ): Psr\Http\Message\ResponseInterface` ### Parameters * **$request** (`Psr\Http\Message\ServerRequestInterface`) - The incoming server request. ### Return: * `Psr\Http\Message\ResponseInterface` - The response to the request. ``` -------------------------------- ### Run Controller Action Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Controller.md Executes a specific controller action identified by its ID, with provided parameters. Throws a RuntimeException if the action cannot be resolved. ```php private runAction(string $id, array $params = []): Psr\Http\Message\ResponseInterface ``` -------------------------------- ### LogEvent Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/LogEvent.md Initializes a new instance of the LogEvent class. Requires a Piko\User\IdentityInterface object. ```php public __construct("Piko\User\IdentityInterface" $identity): mixed ``` -------------------------------- ### Registering Event Listeners with Priority in PHP Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/concepts.md Register a listener for a specific event class using the on() method. Higher priority values execute listeners earlier. ```php $handler->on(string $eventClassName, callable $listener, ?int $priority = null); ``` -------------------------------- ### Render View File Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/View.md Renders a specified view file with an optional model containing data. ```php public function render(string $file, array $model = []): string ``` -------------------------------- ### Registering an Event Listener Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/EventHandlerTrait.md The `on` method registers a callback function to be executed when a specific event is dispatched. You can optionally set a priority for the listener. ```APIDOC ## public **on()** ### Description Registers an event listener. ### Method Signature ```php public on(string $eventClassName, callable $callback, int|null $priority = null): mixed ``` ### Parameters #### Path Parameters - **$eventClassName** (string) - Required - The class name of the event to listen for. - **$callback** (callable) - Required - The callback to execute when the event is dispatched. - **$priority** (int|null) - Optional - Priority for the listener (higher means earlier execution). #### Return: **mixed** ``` -------------------------------- ### initializeSchema() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/DbRecord.md Protected method responsible for initializing the schema for the database table by inspecting class properties with the `FieldAttribute`. ```APIDOC ## initializeSchema(): void ### Description Initialize the schema for the database table. This method uses reflection to inspect the current class for properties that have the `FieldAttribute` attribute. It then builds the schema array, which describes the structure of the table, using these properties. Additionally, it sets the table name if a `TableAttribute` is present on the class and identifies the primary key based on field attributes. ``` -------------------------------- ### getModule Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/ModularApplication.md Retrieves a specific module instance by its identifier. ```APIDOC ## getModule() ### Description Get a module instance by its ID. ### Method public ### Parameters - **$moduleId** (string) - The unique identifier of the module to retrieve. ### Throws: - RuntimeException if the module is not found. ### Return: - **\Piko\Module** An instance of the requested module. ``` -------------------------------- ### Bootstrap Application with I18n Helper Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/i18n.md Register the I18n instance with the global helper in your bootstrap script. This allows safe usage of __() for translations throughout the application. ```php getComponent(I18n::class); I18n::setInstance($I18n); // Now you can safely use __() echo __('app', 'Translation test') . '
'; // Test de traduction echo __('app', 'Hello {name}', ['name' => 'John']) . '
'; // Bonjour John $app->run(); ``` -------------------------------- ### register() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/AssetBundle.md Registers the asset bundle with a specific view instance. This typically involves making the bundle's assets available for rendering within that view. ```APIDOC ## register() ### Description Registers this asset bundle with a view. ### Method public static ### Endpoint N/A (Method Call) ### Parameters #### Path Parameters * **$view** (\Piko\View) - Required - The view to be registered with. ### Request Example ```php $registeredBundle = \Piko\AssetBundle::register($view); ``` ### Response #### Success Response (\Piko\AssetBundle) Returns the registered asset bundle instance. #### Response Example ```json { "example": "\Piko\AssetBundle instance" } ``` ``` -------------------------------- ### BeforeRegisterEvent Constructor Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/BeforeRegisterEvent.md The constructor for the BeforeRegisterEvent. It accepts the asset bundle that is about to be registered. ```php public __construct( Piko\AssetBundle $bundle): mixed ``` -------------------------------- ### load() Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/DbRecord.md Loads record data from the database based on the provided primary key ID. This method returns a static instance and can throw a RuntimeException. ```APIDOC ## load("Piko\number" $id): static ### Description Load row data. ### Parameters #### Path Parameters - **$id** ("Piko\number") - Required - The value of the row primary key. ### Throws - \RuntimeException ### Return: **static** ``` -------------------------------- ### Listen to DbRecord BeforeDeleteEvent Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/db-record.md Attach a listener to the BeforeDeleteEvent to conditionally prevent deletion based on event data. ```php use Piko\DbRecord\Event\BeforeDeleteEvent; $contact = new Contact($db); $contact->on(BeforeDeleteEvent::class, function (BeforeDeleteEvent $event): void { if ($event->record->id === 1) { $event->isValid = false; } }); ``` -------------------------------- ### Set Application Instance - Piko Module Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/api/Module.md Assigns a Piko Modular Application instance to the module. This method is typically used during the application bootstrap process. ```php public setApplication( Piko\ModularApplication $app ): void ``` -------------------------------- ### Creating Objects within a Controller Source: https://github.com/piko-framework/piko-framework.github.io/blob/main/docs/modular-application/controllers.md Uses the controller's `create()` helper to instantiate objects with dependencies, delegating to the module's object factory. ```php public function deleteAction(int $id) { /** @var \app\modules\products\models\Produit $model */ $model = $this->create(\app\modules\products\models\Produit::class); $model->load($id); // ... } ``` ```php $service = $this->create(MyService::class, ['timeout' => 30]); ```