M4W Framework

एप्लिकेशन का निर्माण

शेल
composer create-project mini4work/framework my-app
cd my-app
composer require mini4work/webserver

एप्लिकेशन सर्विस

इस क्लास को app/Services/RequestInfo.php में सेव करें। इसका कंस्ट्रक्टर फ्रेमवर्क के कंटेनर से मौजूदा रिक्वेस्ट लेता है।

PHP
<?php

namespace App\Services;

use M4W\WebServer\Http\Request;

final class RequestInfo
{
    public function __construct(private Request $request) {}

    public function describe(): array
    {
        return ['method' => $this->request->method, 'uri' => $this->request->uri];
    }
}

अनुरोध हैंडलर की संरचना

इस कोड को m4w वाली डायरेक्टरी में server.php के नाम से सेव करें। हर रिक्वेस्ट का अपना कंटेनर होता है। कंटेनर से सर्विस लेने से पहले उसमें रिक्वेस्ट रजिस्टर करें।

PHP
<?php

use App\Services\RequestInfo;
use M4W\Core\Framework;
use M4W\WebServer\Cli\ServerCommand;
use M4W\WebServer\Http\CallbackRequestHandler;
use M4W\WebServer\Http\Request;
use M4W\WebServer\Http\Response;

require __DIR__.'/vendor/autoload.php';

$handler = new CallbackRequestHandler(static function (Request $request): Response {
    $app = new Framework();
    $app->singleton(Request::class, $request);
    return Response::json($app->make(RequestInfo::class)->describe());
});

exit((new ServerCommand)->run($argv, $handler));

सर्वर का स्टार्टअप

इस कोड को server.php वाली डायरेक्टरी में m4w.json के नाम से सेव करें।

JSON
{
  "listen": { "port": 9000 }
}
शेल
php server.php m4w.json

एक दूसरे टर्मिनल में, लूपबैक लिसनर को एक अनुरोध भेजें।

शेल
curl -i 'http://127.0.0.1:9000/inspect?q=hello'
JSON
{"method": "GET", "uri": "/inspect?q=hello"}