PHP code example of adil-jaafar / picophp

1. Go to this page and download the library: Download adil-jaafar/picophp 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/ */

    

adil-jaafar / picophp example snippets


    

    // app/foo/bar/routes.php

    $get = function(Request $request, Response $response) {
        $data = ['message' => 'Hello from /foo/bar'];
        return $response(200)->json($data);
    };

    $post = function(Request $request, Response $response) {
        $body = $request->body();
        // Process the POST data
        return $response(201)->json(['message' => 'Resource created', 'data' => $body]);
    };
    

    

    // app/routes.php

    $get = function(Request $request, Response $response, Env $env, DB $db) {
        $appName = $env('APP_NAME', 'My App');  // Access environment variables
        $users = $db->fetchAll('SELECT * FROM users'); // Access the database
        return $response(200)->json(['app_name' => $appName, 'users' => $users]);
    };
    

    

    // app/users/[id]/edit/routes.php
    $get = function(Path $path, Response $response) {
        $user_id = $path['id'];  // Access the "id" parameter from the URL

        // Fetch user data based on $user_id
        // ...

        return $response(200)->json(['user_id' => $user_id, /* ... */]);
    };


    // app/photo/[...chemin]/routes.php
    $get = function(Path $path, Response $response) {
        $photo_path = $path['chemin'];  // Access the "chemin" parameter from the URL which can contain multiple segments
        // Ex: /photo/path/to/my/image.jpg
        // $photo_path will be equal to "path/to/my/image.jpg"

        // Serve the image at $photo_path
        // ...

        return $response(200); // Or any other response
    };
    

    

    // app/middleware.php

    $before = [
        function(Request $request, Response $response, Env $env) {
            // Check authentication before allowing access
            $apiKey = $request->header('X-API-Key');
            if ($apiKey !== $env('API_KEY')) {
                return $response(401)->json(['error' => 'Unauthorized']);
            }
            return true; // Continue to the route handler (if this returns nothing or is ommited)
        }
    ];

    // app/foo/middleware.php
    $before = [
        function(Request $request, Response $response) {
            // Log something
             error_log('Before foo');
             return true;
        }
    ];

    $after = [
        function(Request $request, Response $response) {
            $response->header('X-Powered-By', 'PicoPHP');
            return true;
        }
    ];
    

    app/
    ├── foo/
    │   ├── bar/
    │   │   └── routes.php   # Defines routes for /foo/bar
    │   └── routes.php       # Defines routes for /foo
    └── routes.php           # Defines routes for the root (/)