Siler ❤️ Swoole
Flat files and plain-old PHP functions rocking on a production-grade, high-performance, scalable, concurrent and non-blocking HTTP server.
Last updated
Was this helpful?
Flat files and plain-old PHP functions rocking on a production-grade, high-performance, scalable, concurrent and non-blocking HTTP server.
Last updated
Was this helpful?
Enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, Websocket services in PHP programming language without too much knowledge about non-blocking I/O programming and low-level Linux kernel. Compared with other async programming frameworks or softwares such as Nginx, Tornado, Node.js, Swoole has the built-in async, multiple threads I/O modules. Developers can use sync or async API to write the applications —
Rapid development of high performance protocol servers & clients with PHP language
Event-driven, asynchronous programming for PHP
Event loop API
Processes management API
Memory management API
for inter-processes communication
Web applications and systems
Mobile communication systems
Online game systems
Internet of things
Car networking
Smart home systems
It is open source and free. Released under the license of Apache 2.0.
Swoole forks a number of worker processes based on CPU core number to utilize all CPU cores.
Swoole supports Long-live connections for websocket server or TCP/UDP server.
Swoole supports more server-side protocols.
Swoole can manage and reuse the status in memory.
You already got Siler, right? Flat-files and plain-old PHP functions rockin'on! Just simple. A set of general purpose high-level abstractions aiming an API for declarative programming in PHP. And this wouldn't be different about Swoole.
The Siler\Swoole
namespace get you covered.
That's it! This attaches a callback handler that always emits "Hello World" on every request and starts a HTTP server on port 9501. Run it using docker-compose up
or just php index.php
if you're not using Docker.
Go to http://localhost:9051
or http://<docker_machine_ip>:9051
and you should get a "Hello World" response as plain/text.
Now we are forwarding GET requests from path /
to file pages/home.php
.
You may ask: "What about Swoole\emit('Not found', 404)
at the end?".
Nice question! Siler\Swoole\emit()
function will short-circuit further emit attempts, so it will work exactly like you have imagined, when a route matches a path like /
it will emit the proper response, but when no route matches and this means: no route will emit something, then Swoole\emit('Not found', 404)
will emit a 404 Not found response.
Twig should work exactly the same as there is no Swoole behind it:
If you're sure that your template doesn't depend on the request, you can render it once:
This avoids the template to be re-rendered on each request unnecessarily.
The Siler\Swoole\http
function returns a plain Swoole\Http\Server
so you can give it to a variable and use regular methods from Swoole's documentation like set
:
Since there is no web server module or CGI layer, things like $_GET won't work for query string parameters etc. But fear nothing, Siler provides getters for both Swoole's Request and Response objects: Siler\Swoole\request()
and Siler\Swoole\response()
.
Instead of always printing "Hello World", let's print the name that came from the URL parameter:
This is as simple as Siler gets.
We can add a new route/API endpoint to GET all of our Todos
:
Then you can return your JSON and within json()
, Siler will automatically add the Content-type: application/json response header. Also you can enable CORS.
Swoole is released as a and runs as a PHP CLI application. The differences between Swoole with PHP-FPM the traditional PHP model are:
Swoole prerequisites operation system are: Linux, FreeBSD or MacOS, but don't worry Windows-people, we have ! And I got us covered with :
You know, Siler can do a lot more, it abstracts things like and . Let's add this to our Swoole server:
Go ahead, restart the server and go to , you should still be seeing "Hello World", but going to any other path, like , you should be seeing "Not found" and a proper 404 status code.
Go to , you should be seeing "Hello Leo" now.
You can find more about Swoole's Request and Response objects at:
Head to . There we go! A Siler ❤️ Swoole powered API.