HTTP Controller

all controller must be a sub class of EasySwoole\Core\Http\AbstractInterface\Controller(abstract class),and must implement index method.

Controller Namespace

all of the controller name must be CamelCase,and below the namespace of App\HttpController.For example :


namespace App\HttpController;

use EasySwoole\Core\Http\AbstractInterface\Controller;

class Hello extends Controller
    function index()
        $this->response()->write('Hello easySwoole!');

access url is :/hello/index.html

To Implement method

index method must be implement in your controller.

Controller Default Methods And How It Work

  • protected function actionNotFound($action):void
  • protected function afterAction($action):void
  • protected function onException(\Throwable $throwable,$actionName):void
  • protected function onRequest($action):?bool //you can intercept an request to prevent do the next action here
  • protected function request():Request
  • protected function response():Response
  • protected function writeJson($statusCode = 200,$result = null,$msg = null)


__construct will call in dispatch ,$request and $response is all the implement of psr-7 http message

public function __construct(string $actionName,Request $request,Response $response)
    $this->request = $request;
    $this->response = $response;
    $this->actionName = $actionName;
    if($actionName == '__construct'){
        $this->__hook( $actionName);


protected function __hook(?string $actionName):void
    // onRequest if rerurn false ,means do not exec the next method
    if($this->onRequest($actionName) !== false){
            $ref = new \ReflectionClass(static::class);
            //if method is not public ,
            if($ref->hasMethod($actionName) && $ref->getMethod( $actionName)->isPublic()){
       }catch (\Throwable $throwable){
           //you can overwrite onException method to prevent throw a Throwable in a http request
       }catch (\Throwable $throwable){

The Url Rule to Controller

EasySwoole use PATH_INFO mode in url parser。 for example ,your access url :


and the follow parser rule:

  • if has 'App/HttpController/Api/Auth' controller ,then url match,action name is index,
  • else if has 'App/HttpController/Api/Index' controller,then url match,action name is auth,
  • else if has 'App/HttpController/Api' controller,then url match,action name is auth,
  • else if has 'App/HttpController/Index' controller,then url match,action name is api,
  • else if not any controller,show easySwoole default welcome page.

The Url Max Depth

EasySwoole parser three depth or url path for the default setting,you can change it at EasySwooleEvent.php

public static function frameInitialize(): void
    Di::getInstance()->set(SysConst::HTTP_CONTROLLER_MAX_DEPTH, MAX_NUM);

URL Parser is Case Sensitive (ucfirst)

results matching ""

    No results matching ""