02
AugStatix Server is a fantastic package introduced by Statix-php that offers an object oriented wrapper around PHP’s built-in server.
You can install this package via composer by running this command in your terminal
composer require statix/server
To get started, ensure the vendor autoload script is required and then create an instance of the Server class, once you have set any configuration options, you should call the start method to start the server.
use Statix\Server\Server;
require_once './vendor/autoload.php';
Server::new()->start();
// or
(new Server)->start();
You can configure several options with the server, such as the host, the port, the root directory, and more. Please read more below for a detailed explanation of each configuration method.
Passing configuration via the constructor or Server::new()
You may pass most configuration options via the constructor. For example, we are setting the host, port, and root options in the code below.
use Statix\Server\Server;
Server::new([
'host' => 'localhost',
'port' => 8000,
'root' => __DIR__ . '/content'
]);
// or
new Server([
'host' => 'localhost',
'port' => 8000,
'root' => __DIR__ . '/content'
]);
The complete list of configuration items that can be passed via the constructor can be found below.
$optionsSettableViaContructor = [
'host' => 'string', // default: localhost
'port' => 'string|int', // default: 8000
'root' => 'string', // default: getcwd()
'router' => 'string', // path to your routing script
'executable' => 'string', // path to the desired PHP binary to use for the server
'withEnvVars' => [
'APP_DYNAMIC_ENV' => 'server'
],
'withoutEnvVars' => [
'APP_KEY'
]
];
You also have the option of calling named methods to set the configuration options as shown below.
use Statix\Server\Server;
Server::new()
->usePHP('path')
->onHost('localhost')
->onPort('8080')
->root('./content')
->useRouter('./router.php')
->withEnvVars([
'APP_DYNAMIC_ENV' => 'server'
])->withoutEnvVars([
'APP_KEY',
]);
If you want to show the output from the server process as it receives and handles requests, you may call the output method and pass a callback function that will be called and passed any output of the process.
Server::new()
->output(function($output) {
echo $output;
})->start();
You may find it useful to run the server process in the background, you may call runInBackground()
. The process will run as long as the parent script is running.
Server::new()->runInBackground();
You may check whether or not the server is currently running by calling the isRunning method.
$server = Server::new()->withEnvVars([
'APP_NAME' => 'statix/server',
]);
$server->isRunning(); // false
$server->runInBackground();
$server->isRunning(); // true
You may stop the process of running the server by calling the stop command on an instance of the server class. If the server is not currently running this method will return null otherwise it will return an array container first the process exit code and second the process exit text. Note this command can only be called when the server is running in the background.
$server = Server::new()->runInBackground();
// do work
$server->stop();
For more details, you can visit Github
Published at : 02-08-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project