{"id":408,"date":"2024-07-01T13:00:07","date_gmt":"2024-07-01T13:00:07","guid":{"rendered":"https:\/\/upprofits.net\/?p=408"},"modified":"2024-08-30T11:26:27","modified_gmt":"2024-08-30T11:26:27","slug":"introduction-to-frameworkx","status":"publish","type":"post","link":"https:\/\/upprofits.net\/index.php\/2024\/07\/01\/introduction-to-frameworkx\/","title":{"rendered":"Introduction to FrameworkX"},"content":{"rendered":"

PHP has come a long way and continues to improve with new features, syntax, and speed. The ecosystem is also expanding, with many developers creating frameworks to simplify the lives of other developers. Popular, full-featured frameworks like Laravel and Symfony exist, as do lightweight microframeworks like FrameworkX<\/a><\/strong>.<\/p>\n

It is a lightweight microframework for PHP that uses an event-driven, non-blocking architecture, similar to Node.js which is perfect for high-concurrency and real-time applications like chat apps or live notifications.<\/p>\n

In this article, we will explore what FrameworkX is and how it differs from traditional PHP frameworks. Let\u2019s get started!<\/p>\n

Getting Started<\/h4>\n

First, make sure you have PHP and Composer<\/a> set up on your computer. Once installed, you can add FrameworkX<\/strong> to your project with this command:<\/p>\n

\r\ncomposer require clue\/framework-x\r\n<\/pre>\n

FrameworkX<\/strong> doesn\u2019t require a complex setup. All you need is a public\/index.php<\/strong> file. Here\u2019s a basic example to display \u201cHello World!\u201d on the homepage:<\/p>\n

\r\n<?php\r\nrequire dirname(__DIR__) . '\/vendor\/autoload.php';\r\n$app = new FrameworkXApp();\r\n$app->get('\/', fn () => ReactHttpMessageResponse::plaintext(\"Hello world!n\"));\r\n$app->run();\r\n<\/pre>\n

To run your application, type:<\/p>\n

\r\nphp public\/index.php\r\n<\/pre>\n

This command starts a local server using PHP\u2019s built-in server, backed by the ReactPHP Socket<\/a> component. No need for Nginx or Apache. Your server will run at http:\/\/127.0.0.1:8080<\/code>, and it should display \u201cHello World!\u201d.<\/p>\n

\"Localhost <\/figure>\n

Besides plain text, you can also return JSON data. For example:<\/p>\n

\r\n<?php\r\nrequire dirname(__DIR__) . '\/vendor\/autoload.php';\r\n$users = [['name' => 'Jon Doe'], ['name' => 'Jane Doe']];\r\n\r\n$app = new FrameworkXApp();\r\n$app->get('\/', fn () => ReactHttpMessageResponse::json($users));\r\n$app->run();\r\n<\/pre>\n

Async Operations<\/h4>\n

PHP operations are usually blocking and synchronous, meaning each task must finish before the next one starts. FrameworkX<\/strong> is built on the ReactPHP library<\/a>.<\/p>\n

ReactPHP is a library that provides components such as the EventLoop<\/a>, Stream<\/a>, Promise<\/a>, Async<\/a>, and HTTP<\/a> components, which enable asynchronous operations. Thus, tasks can run concurrently without waiting for others to finish. This is ideal for handling multiple connections, HTTP requests, or I\/O operations simultaneously.<\/p>\n

In this example, we\u2019ve updated our index.php<\/strong> to fetch an API. Instead of using the curl_*<\/code> functions, we will use the HTTP component<\/a> to make an asynchronous request.<\/p>\n

\r\n$app = new FrameworkXApp();\r\n$app->get('\/', function () {\r\n    echo \"Startn\";\r\n    (new ReactHttpBrowser())\r\n        ->get('https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts')\r\n        ->then(function () {\r\n            echo \"End (API)n\";\r\n        });\r\n    echo \"Endn\";\r\n\r\n    return ReactHttpMessageResponse::plaintext(\"Hello world!n\");\r\n});\r\n$app->run();\r\n<\/pre>\n

Normally, an external API request would block the page from rendering until the request completes. However, with the asynchronous operations that the ReactPHP HTTP component handles, the page loads instantly, as evidenced by the log.<\/p>\n

\"Async <\/figure>\n

This makes FrameworkX capable of handling many more concurrent requests than traditional PHP setups, significantly speeding up page load times. But how fast is it?<\/p>\n

Speed<\/h4>\n

I tested FrameworkX<\/strong> on a basic, inexpensive DigitalOcean droplet with 1 vCPU and 1GB of RAM. It handled around 4,000 requests per second effortlessly.<\/p>\n

\r\nConcurrency Level:      50\r\nTime taken for tests:   22.636 seconds\r\nComplete requests:      100000\r\nFailed requests:        0\r\nKeep-Alive requests:    100000\r\nTotal transferred:      17400000 bytes\r\nHTML transferred:       1300000 bytes\r\nRequests per second:    4417.69 [#\/sec] (mean)\r\nTime per request:       11.318 [ms] (mean)\r\nTime per request:       0.226 [ms] (mean, across all concurrent requests)\r\nTransfer rate:          750.66 [Kbytes\/sec] received\r\n<\/pre>\n

Even with additional workload, like disk read operations and rendering 100 lists from a JSON file, it still managed around 2700 requests per second.<\/p>\n

\r\nConcurrency Level:      50\r\nTime taken for tests:   36.381 seconds\r\nComplete requests:      100000\r\nFailed requests:        0\r\nKeep-Alive requests:    100000\r\nTotal transferred:      296700000 bytes\r\nHTML transferred:       280500000 bytes\r\nRequests per second:    2748.72 [#\/sec] (mean)\r\nTime per request:       18.190 [ms] (mean)\r\nTime per request:       0.364 [ms] (mean, across all concurrent requests)\r\nTransfer rate:          7964.31 [Kbytes\/sec] received\r\n<\/pre>\n

I\u2019m pretty sure it could be much faster with better server specifications.<\/p>\n

Wrapping Up<\/h4>\n

FrameworkX<\/strong> is a powerful, lightweight microframework for PHP. It runs asynchronously and is capable of handling multiple tasks efficiently, similar to Node.js. It\u2019s the perfect framework whether you\u2019re building a simple app or complex high-concurrency or real-time applications.<\/p>\n

The post Introduction to FrameworkX<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

PHP has come a long way and continues to improve with new features, syntax, and speed. The ecosystem is also expanding, with many developers creating frameworks to simplify the lives of other developers. Popular, full-featured frameworks like Laravel and Symfony exist, as do lightweight microframeworks like FrameworkX. It is a lightweight microframework for PHP that […]<\/p>\n","protected":false},"author":1,"featured_media":411,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/408"}],"collection":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/comments?post=408"}],"version-history":[{"count":3,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/408\/revisions\/418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media\/411"}],"wp:attachment":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}