Creation to FrameworkX

by | Jul 1, 2024 | Etcetera | 0 comments

PHP has come some distance and continues to toughen with new choices, syntax, and speed. The ecosystem is also expanding, with many developers rising frameworks to simplify the lives of various developers. Well-liked, full-featured frameworks like Laravel and Symfony exist, as do lightweight microframeworks like FrameworkX.

It is a lightweight microframework for PHP that uses an event-driven, non-blocking construction, similar to Node.js which is absolute best imaginable for high-concurrency and real-time systems like chat apps or live notifications.

In this article, we will uncover what FrameworkX is and how it differs from typical PHP frameworks. Let’s get started!

Getting Started

First, you’ll wish to have PHP and Composer organize in your laptop. Once installed, you’ll add FrameworkX in your mission with this command:

composer require clue/framework-x

FrameworkX doesn’t require a complicated setup. All you need is a public/index.php report. Proper right here’s a elementary example to turn “Hello International!” on the homepage:

get('/', fn () => ReactHttpMessageResponse::plaintext("Hello world!n"));
$app->run();

To run your tool, sort:

php public/index.php

This command starts an area server the use of PHP’s built-in server, subsidized by the use of the ReactPHP Socket part. No need for Nginx or Apache. Your server will run at http://127.0.0.1:8080, and it’s going to have to turn “Hello International!”.

Localhost server setupLocalhost server setup

Besides simple text, you’ll moreover return JSON wisdom. For example:

 'Jon Doe'], ['name' => 'Jane Doe']];

$app = new FrameworkXApp();
$app->get('/', fn () => ReactHttpMessageResponse::json($consumers));
$app->run();

Async Operations

PHP operations are usually blocking and synchronous, this means that each process should finish previous than the next one starts. FrameworkX is built on the ReactPHP library.

See also  9 Best possible Etsy Choices for Dealers (Extra Freedom + Much less Charges)

ReactPHP is a library that provides parts such since the EventLoop, Circulation, Promise, Async, and HTTP parts, which enable asynchronous operations. Thus, tasks can run at the same time as without having a look ahead to others to finish. This is best for coping with multiple connections, HTTP requests, or I/O operations at the same time as.

In this example, we’ve up to the moment our index.php to fetch an API. Instead of the use of the curl_* functions, we will use the HTTP element to make an asynchronous request.

$app = new FrameworkXApp();
$app->get('/', function () {
    echo "Startn";
    (new ReactHttpBrowser())
        ->get('https://www.hongkiat.com/blog/wp-json/wp/v2/posts')
        ->then(function () {
            echo "End (API)n";
        });
    echo "Endn";

    return ReactHttpMessageResponse::plaintext("Hello world!n");
});
$app->run();

Maximum incessantly, an external API request would block the internet web page from rendering until the request completes. Then again, with the asynchronous operations that the ReactPHP HTTP part handles, the internet web page fairly so much immediately, as evidenced by the use of the log.

Async operations logAsync operations log

This makes FrameworkX in a position to coping with many additional concurrent requests than typical PHP setups, significantly speeding up internet web page load circumstances. Then again how fast is it?

Pace

I tested FrameworkX on a elementary, inexpensive DigitalOcean droplet with 1 vCPU and 1GB of RAM. It handled spherical 4,000 requests in keeping with second simply.

Concurrency Level:      50
Time taken for tests:   22.636 seconds
Complete requests:      100000
Failed requests:        0
Keep-Alive requests:    100000
Common transferred:      17400000 bytes
HTML transferred:       1300000 bytes
Requests in keeping with second:    4417.69 [#/sec] (indicate)
Time in keeping with request:       11.318 [ms] (indicate)
Time in keeping with request:       0.226 [ms] (indicate, all the way through all concurrent requests)
Transfer rate:          750.66 [Kbytes/sec] won

Even with additional workload, like disk be told operations and rendering 100 lists from a JSON report, it nevertheless managed spherical 2700 requests in keeping with second.

Concurrency Level:      50
Time taken for tests:   36.381 seconds
Complete requests:      100000
Failed requests:        0
Keep-Alive requests:    100000
Common transferred:      296700000 bytes
HTML transferred:       280500000 bytes
Requests in keeping with second:    2748.72 [#/sec] (indicate)
Time in keeping with request:       18.190 [ms] (indicate)
Time in keeping with request:       0.364 [ms] (indicate, all the way through all concurrent requests)
Transfer rate:          7964.31 [Kbytes/sec] won

I’m beautiful positive it may well be so much sooner with upper server specifications.

See also  A Amateur’s Information to Growing an On-line Direction

Wrapping Up

FrameworkX is an excellent, lightweight microframework for PHP. It runs asynchronously and is in a position to coping with multiple tasks effectively, similar to Node.js. It’s the perfect framework whether or not or now not you’re development a simple app or difficult high-concurrency or real-time systems.

The put up Creation to FrameworkX seemed first on Hongkiat.

WordPress Website Development

Supply: https://www.hongkiat.com/blog/frameworkx/

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!