Download the PHP package adil-jaafar/picophp without Composer
On this page you can find all versions of the php package adil-jaafar/picophp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adil-jaafar/picophp
More information about adil-jaafar/picophp
Files in adil-jaafar/picophp
Package picophp
Short Description Ultra-léger, conçu pour aller à l'essentiel.
License MIT
Informations about the package picophp
picoPHP - Your Lightweight and Flexible PHP Framework
picoPHP is a minimalistic yet powerful PHP framework designed for rapid development and ease of use. It leverages file-based routing, dependency injection, and middleware to provide a clean and organized structure for your web applications.
Key Features
- File-Based Routing: Define your routes simply by creating
routes.php
files within your application's directory structure. - Dependency Injection: Automatically inject services (like Request, Response, Env, DB) into your route handlers for easy access and testing.
- Path Parameters: Easily capture dynamic path segments as parameters within your routes.
- Middleware: Implement reusable logic that can be applied before and after route handlers, allowing you to easily manage authentication, authorization, data validation, and more.
- HTTP Method Handling: Easily define handlers for different HTTP methods (GET, POST, PUT, DELETE, etc.)
Getting Started
-
Installation: Use Composer to install picoPHP.
-
Directory Structure: Organize your application code within the
app
directory. -
Routing with
routes.php
: Createroutes.php
files to define your routes.This example defines a
GET
andPOST
route for/foo/bar
. -
Accessing Services: picoPHP automatically injects the required services into your route handlers.
-
Path Parameters: Use
Path
service to access route parameters defined with square brackets in the directory structure./users/123/edit
will map to the function inapp/users/[id]/edit/routes.php
and the parameter 'id' will equal123
./photo/path/to/my/image.jpg
will map to the function inapp/photo/[...chemin]/routes.php
and the parameter 'chemin' will equalpath/to/my/image.jpg
.
-
Directory Naming Convention Folders enclosed in parentheses
()
are intended for structural organization, e.g.,app/users/(admin)/edit/[user_id]
. They are used to map paths like/users/edit/123
to the route structure, whereuser_id
captures the dynamic parameter. -
Middleware: Add middleware to
middleware.php
files in any directory within your application. Middleware allows you to execute code before and after route handlers.$before
: An array of functions to execute before the route handler.- If any
$before
middleware function returns a value (other thantrue
ornull
), that value is considered the response, and the route handler is not executed. - Returning
true
(or nothing at all) from a$before
middleware function allows the request to proceed to the route handler (and to other$before
middleware further down in the application's folder structure) - If a
$before
middleware returns aResponse
object, processing stops, and the framework immediately returns this response. This is ideal for things like auth checks.
- If any
$after
: An array of functions to execute after the route handler.$after
middleware run in the reverse order from the$before
middleware.
Middleware Execution Order:
$before
middleware functions are executed from the root directory down to the directory containing theroutes.php
file. If abefore
returns a value other than true, processing stops.- The route handler function in
routes.php
is executed. $after
middleware functions are executed from the directory containing theroutes.php
file up to the root directory.
Example: Authentication Middleware
Create a middleware.php
file in your app
directory to implement global authentication.
<?php
// app/middleware.php
$before = [
function(Request $request, Response $response) {
if (!isAuthenticated($request)) {
return $response(401)->json(['error' => 'Unauthorized']);
}
}
];
function isAuthenticated(Request $request): bool {
//TODO replace by a real auth check
$token = $request->header("authorization");
return $token === "Bearer mysecrettoken";
}