Create and Run a Webman Project
What is Webman?
Webman is a high-performance PHP asynchronous web framework based on Workerman, designed for building high-concurrency, high-performance web applications. Unlike traditional synchronous blocking frameworks, Webman uses event-driven and asynchronous non-blocking I/O, excelling at handling large numbers of concurrent requests. Webman provides a simple and easy-to-use API and flexible extension mechanisms, making it very suitable for building real-time applications, API services, etc.
Main Features and Advantages of Webman
- High Performance: Utilizes event-driven and asynchronous non-blocking I/O to handle large numbers of concurrent requests.
- Ease of Use: Provides a simple API and rich features, allowing developers to quickly get started.
- Multi-Protocol Support: Supports multiple protocols such as HTTP, WebSocket, making it suitable for various application scenarios.
- Flexible Extension: Allows functionality to be extended through plugins and middleware mechanisms.
- Strong Community Support: Has an active developer community and rich documentation resources.
Webman helps developers quickly build high-performance web applications and API services, suitable for various scenarios that require high concurrency handling.
Create and Run a Simple Web Project Using Webman
In this article, we will demonstrate how to create and run a simple web project using Webman in a ServBay environment. We will show how to install Webman, write basic routing and controller code, and run the project.
TIP
ServBay suggests placing websites in the /Applications/ServBay/www
directory for easier management.
Install Webman
Install Composer
ServBay comes with Composer pre-installed, so you do not need to install it separately.
Create Project Directory
Navigate to the www directory of ServBay:
bashcd /Applications/ServBay/www
1Install Webman
Use Composer to install Webman:
bashcomposer create-project workerman/webman servbay-webman-app cd servbay-webman-app
1
2Install Required Components
Install Illuminate database, pagination, events, and Symfony VarDumper:
bashcomposer require -W illuminate/database illuminate/redis illuminate/pagination illuminate/events symfony/var-dumper
1
Write Web Project Code
Configure Routing
Add the following code in the
config/route.php
file to define basic routes:phpuse Webman\Route; use app\controller\IndexController; use app\controller\CacheController; use app\controller\DatabaseController; Route::any('/', [IndexController::class, 'index']); Route::any('/memcached', [CacheController::class, 'memcached']); Route::any('/redis', [CacheController::class, 'redis']); Route::any('/mysql-add', [DatabaseController::class, 'mysqlAdd']); Route::any('/mysql', [DatabaseController::class, 'mysqlGet']); Route::any('/pgsql-add', [DatabaseController::class, 'pgsqlAdd']); Route::any('/pgsql', [DatabaseController::class, 'pgsqlGet']);
1
2
3
4
5
6
7
8
9
10
11
12Create Controllers
Create the
IndexController.php
,CacheController.php
, andDatabaseController.php
files in theapp/controller
directory and add the following code:IndexController.php
file:phpnamespace app\controller; use support\Request; class IndexController { public function index(Request $request) { return response('Hello ServBay!'); } }
1
2
3
4
5
6
7
8
9
10
11CacheController.php
file:phpnamespace app\controller; use support\Request; use support\Response; use Memcached; use support\Redis; class CacheController { public function memcached(Request $request): Response { $memcached = new Memcached(); $memcached->addServer('127.0.0.1', 11211); $memcached->set('key', 'Hello Memcached!', 60); $value = $memcached->get('key'); return response($value); } public function redis(Request $request): Response { Redis::set('key', 'Hello Redis!'); $value = Redis::get('key'); return response($value); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25DatabaseController.php
file:phpnamespace app\controller; use support\Request; use support\Response; use support\Db; class DatabaseController { public function mysqlAdd(Request $request): Response { DB::connection('mysql')->table('users')->insert([ 'name' => 'Webman', 'email' => '[email protected]', ]); return response('User added'); } public function mysqlGet(Request $request): Response { $users = DB::connection('mysql')->table('users')->get(); return response(json_encode($users)); } public function pgsqlAdd(Request $request): Response { DB::connection('pgsql')->table('users')->insert([ 'name' => 'Webman', 'email' => '[email protected]', ]); return response('User added'); } public function pgsqlGet(Request $request): Response { $users = DB::connection('pgsql')->table('users')->get(); return response(json_encode($users)); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38Configure Database Connection
Configure the connection settings of MySQL and PostgreSQL in the
config/database.php
file:phpreturn [ 'default' => 'mysql', 'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => '127.0.0.1', 'port' => 3306, 'database' => 'webman_app', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], 'pgsql' => [ 'driver' => 'pgsql', 'host' => '127.0.0.1', 'port' => 5432, 'database' => 'webman_app', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', 'sslmode' => 'prefer', ], ], ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Run the Web Project
In the project directory, run the following command to start the Webman project:
php start.php start
After starting, you can access the following URLs in your browser:
http://localhost:8787
: You will see the page outputHello ServBay!
.http://localhost:8787/memcached
: You will see the page outputHello Memcached!
.http://localhost:8787/redis
: You will see the page outputHello Redis!
.http://localhost:8787/mysql-add
: You will see the page outputUser added
and a user will be added to the database.http://localhost:8787/mysql
: You will see the user list in the database.http://localhost:8787/pgsql-add
: You will see the page outputUser added
and a user will be added to the database.http://localhost:8787/pgsql
: You will see the user list in the database.
Summary
By following these steps, you have successfully created and run a Webman project through ServBay, using the features provided by Webman to manage and access your project while connecting to various databases and calling data. The high performance and ease of use of Webman make it very suitable for building high-concurrency, high-performance web applications and API services. We hope this article helps you quickly get started with Webman and apply it to your projects.