char101 / workerlib
Installs: 7
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/char101/workerlib
Requires
- erusev/parsedown: ^1.7
- fenom/fenom: ^2.12
- nikic/fast-route: ^1.3
- phug/phug: ^1.8
- scssphp/scssphp: ^1.5
- symfony/var-dumper: ^5.3
- workerman/workerman: dev-master
This package is auto-updated.
Last update: 2025-12-29 03:53:35 UTC
README
Description
Utility classes for Workerman.
Requirements
- PHP >= 8.0
- event extension
- redis extension
Installation
Set minimum stability to dev in composer.json.
If there is no existing composer.json:
echo '{"minimum-stability": "dev"}' > composer.json
Or add to existing composer.json:
{
"minimum-stability": "dev"
}
Install the package:
composer require char101/workerlib
Create main.php as the application entry point:
<?php require __DIR__.'vendor/autoload.php'; $app = new App(function($app) { // Code in here will be run in the `onStart` worker event handler // and can be reloaded using Workerman reload command. // Include custom PHP files here. }); $app->run();
Configure application:
cp vendor/char101/workerlib/config.yaml .
Open config.yaml and configure the values.
Running the Server
Running the server:
php main.php start
Reloading the code:
php main.php reload
NOTE: reloading only works for code specified inside the closure given to new App.
Restarting the code
php main.php restart
Restarting will also reload the code change in main.php.
Routing
Route can be registered in 3 ways:
Using the route method with a closure
<?php require __DIR__.'/vendor/autoload.php'; $app = new App(); $app->route('/user/{id}', function($id) { return ['id' => id]; }); $app->run();
Using the route method with a class
classes/Controller/User.php
class Controller_User extends Controller { public function view($id) { return ['id' => $id]; } }
require __DIR__.'/vendor/autoload.php'; $app = new App(); $app->route('/user', Controller_User::class, [ 'GET /{id}' => 'view', 'POST /{id}' => 'saveEdit' ]); $app->run();
Using annotations
classes/Controller/User.php
#[Route] class Controller_User extends Controller { #[Route('GET /{id}'] public function view($id) { return ['id' => $id]; } #[Route('POST /{id}')] public function saveEdit($id) { return this->redirect('/user/'.$id); } }
Conventions
- Route handler return types
- text ->
text/plain - array/object ->
application/json - Response -> HTTP Response
- text ->
#[Route] equals to #[Route('/class_name')] for class or #[Route('GET /method_name')] for
method.
#[Route('/url')] equals to #[Route('GET /url')] for method.
Directory Layout
app/
composer.json
main.php
classes/
Controller/
User.php
templates/
layout.pug
user/
login.pug
vendor/
App
The App class is the application instance that initializes and run Workerman.
Controller
Create new controller in classes\controller\[Class].php or
Controller\[Class].php.
Template
Create new template in templates\[controller_name_in_snake_case].pug.
Database
Configure PDO URL in config.yaml.
SQL
Create [Controller].sql in the same directory as [Controller].php then load
the SQLLoader service using $sql parameter.
classes/Controller/User.php:
<?php class Controller_User extends Controller { public function index($sql) { return $sql->users(); } }
classes/Controller/User.sql:
--: users: all SELECT * FROM user
The format of SQL identifier is --: {name}: {return type}. {return type}
is optional, if not specified it will default to execute. The available
{return type}s refers to the method of DB class.
execute: execute the statement and returns theStatementobjectone: returns a scalar value of the first column of the first rowrow: returns a single rowcol: returns the values of the first column of all rowsall: returns all rowsmap: returns all rows as associated array with the first column as the array key
Helper Methods
insert:$db->insert('table', ['col' => 'value'], 'returning id');update:$db->update('table', ['col' => 'value'], ['where' => 'value']);delete:$db->update('table', ['where' => 'value'], 'returning *');list:$db->list([1, 2, 3]);raw:['created' => DB::raw('CURRENT_TIMESTAMP')]
Migrations
Not available yet.
Other Services
Redis
$redis = RedisDB::instance('cache'); $redis->set('key', 'value');
LDAP
Development
Auto reload server on file change using watchexec:
watchexec --restart --no-ignore --exts php,pug,yaml --ignore public -- php main.php reload