title: Hello World meta:

  • name: description Content: EasySwoole hello world development example
  • name: keywords Content: swoole|swoole extension|swoole framework|easyswoole|swoole extension|swoole framework|php coroutine framework

Hello World

Create the following directory structure in the project root directory. This directory is the application directory for writing business logic. Edit the Index.php file and add the code of the base controller.

::: tip Note that the directory will not be automatically generated after installation (this is different from the traditional framework), you need to create and add composer to load the configuration to take effect. Please read this content patiently. :::

  1. project Project deployment directory
  2. ----------------------------------
  3. ├─App Application directory
  4. └─HttpController Application controller directory
  5. └─Index.php Default controller file
  6. ----------------------------------
  1. <?php
  2. namespace App\HttpController;
  3. use EasySwoole\Http\AbstractInterface\Controller;
  4. class Index extends Controller
  5. {
  6. function index()
  7. {
  8. // TODO: Implement index() method.
  9. $this->response()->write('hello world');
  10. }
  11. }

Then edit the composer.json file in the root directory to register the application’s namespace.

  1. {
  2. "autoload": {
  3. "psr-4": {
  4. "App\\": "App/"
  5. }
  6. },
  7. "require": {
  8. "easyswoole/easyswoole": "3.x"
  9. }
  10. }

::: warning In fact, it is the name space of the registered app. :::

Finally, the composer dumpautoload command is executed to update the namespace. The framework can automatically load the files in the App directory. At this point, the framework has been installed and you can start writing business logic.

  1. # Update namespace mapping
  2. composer dumpautoload
  3. # Start frame
  4. php easyswoole start

After launching the framework, visit http://localhost:9501 to see Hello World.

About IDE Assistant

Since the function of Swoole is not a PHP standard function, the IDE cannot perform auto-completion. In order to facilitate development, you can execute the following command to introduce the IDE assistant, and the Swoole-related functions can be automatically completed under the IDE.

  1. composer require easyswoole/swoole-ide-helper

Directory Structure

EasySwoole‘s directory structure is very flexible, basically can be customized, there are not many constraints, but for the convenience of development, it is still recommended to follow the following directory structure.

  1. project Project deployment directory
  2. ├─App Application directory (can have multiple)
  3. ├─HttpController Controller directory
  4. └─Index.php Default controller
  5. └─Model Model file directory
  6. ├─Log Log file directory
  7. ├─Temp Temporary file directory
  8. ├─vendor Third-party class library directory
  9. ├─composer.json Composer architecture
  10. ├─composer.lock Composer lock
  11. ├─EasySwooleEvent.php Framework global event
  12. ├─easyswoole Framework management script
  13. ├─dev.php Development configuration file
  14. ├─produce.php Production profile

::: warning If the project also needs to use other static resource files, it is recommended to use Nginx / Apache as the front-end web service, forward the request to easySwoole for processing, and add a Public directory as the root directory of the web server. :::

::: warning Note! Please do not use the framework home directory as the root directory of the web server, otherwise the root directory file configuration such as dev.php,produce.php will be accessible, or you can exclude important files by yourself. :::