title: Controller meta:

  • name: description content: Easyswoole controller description
  • name: keywords content: swoole|swoole extension|swoole framework|easyswoole|Controller|Swoole api service|Swoole controller pool

Controller object

The controller object is an object in the http component that facilitates interaction between the client and the server. It uses the object pool object reuse pattern and injects request and response objects for data interaction.

Object pool mode

The http controller object uses the object pool mode to get the created object. E.g:

  • User A requests /Index through url parsing and routing forwarding, and locates the App\HttpController\Index.php controller.
  • Since it is the first request, new App\HttpController\Index.php and save the object in the object pool
  • The object pool is dequeued, the object is obtained, and the index method is called to process the request.
  • After processing, reset the object’s properties to the default value, the object recycles the object pool
  • User B requests /Index through url parsing and routing forwarding, and locates the App\HttpController\Index.php controller
  • Because it is a secondary request, the object pool directly obtains the first object, does not need new, directly calls the index method for processing

::: warning The object pool mode implements different requests to reuse the same object, reducing the overhead of creating/destroying objects. The constructor is called only when the object is requested for the first time, and will not be called again when the object is acquired for the second time. The pool mode does not reset the static attribute and the private private attribute. These two attributes will be reused. The object pool mode is for a single process, and the object pools of multiple work processes are not shared. :::

Convention specification

  • The class name and class file (folder) in the project are named, both are big hump, and the variable and class method are small hump.
  • In the HTTP response, echo $var in the business logic code does not output the $var content to the corresponding content. Please call the wirte() method in the Response instance.

object method

Scheduling class method

  • action

    action is the final method executed by the controller. According to the matching of the routes, different controller methods are executed, such as the default index method, such as the test method that accesses the final parsing of ip/Index/test. , can be called action execution method.

::: warning The action method can return a string, allowing the framework to schedule controller methods again, for example: :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Tioncico
  5. * Date: 2019/4/11 0011
  6. * Time: 14:40
  7. */
  8. namespace App\HttpController;
  9. use EasySwoole\EasySwoole\Trigger;
  10. use EasySwoole\Http\AbstractInterface\Controller;
  11. use EasySwoole\Http\Message\Status;
  12. class Index extends Controller
  13. {
  14. function index()
  15. {
  16. $this->writeJson(200, [], 'success');
  17. return '/test';
  18. }
  19. function test()
  20. {
  21. $this->response()->write('this is test');
  22. return '/test2';//After executing the test method, return /test2 to let the framework continue to dispatch the /test2 method.
  23. }
  24. function test2()
  25. {
  26. $this->response()->write('this is test2');
  27. return true;
  28. }
  29. }

::: warning The returned string will be parsed by the url parsing rule and the route route rule, but it should be noted that the A method must not return the B method, and the B method returns the string of the A method, otherwise an infinite loop call will occur. :::

  • onRequest
  1. protected function onRequest(?string $action): ?bool
  2. {
  3. return true;
  4. }

The event when the controller method is ready to process the request. If the method returns false, it will not continue to execute. Can be used to do controller base class permission verification, etc., for example:

  1. function onRequest(?string $action): ?bool
  2. {
  3. if (parent::onRequest($action)) {
  4. //Determine whether to log in
  5. if (1/*Fake code*/) {
  6. $this->writeJson(Status::CODE_UNAUTHORIZED, '', 'Login has expired');
  7. return false;
  8. }
  9. return true;
  10. }
  11. return false;
  12. }
  • afterAction
    This method will be called when the controller method finishes executing, and the logic such as data recovery can be customized.

  • index Index is an abstract method that represents the inheritance of the controller object and needs to implement the method. index will become the default controller method.

  • actionNotFound When the request method is not found, the method is called automatically, and the method can be overridden to implement its own logic.

::: warning This method can be understood as default method, similar to the index method, so after the call, it will also trigger afterAction, gc and other methods. :::

  • onException This method is called when the controller logic throws an exception (the framework has already handled the exception by default) You can override this method to perform custom exception handling, for example:
    1. function onException(\Throwable $throwable): void
    2. {
    3. // Directly respond to the front end 500 and output the system busy
    4. $this->response()->withStatus(Status::CODE_INTERNAL_SERVER_ERROR);
    5. $this->response()->write('The system is busy, please try again later');
    6. }

::: warning More controller exceptions can be found in [Error and Exception Interception] (exception.md) :::

  • gc
    1. protected function gc()
    2. {
    3. // TODO: Implement gc() method.
    4. if ($this->session instanceof SessionDriver) {
    5. $this->session->writeClose();
    6. $this->session = null;
    7. }
    8. //Restore Defaults
    9. foreach ($this->defaultProperties as $property => $value) {
    10. $this->$property = $value;
    11. }
    12. }
    The gc method will be called automatically after executing method, afterAction Reset controller properties to default values, turn off session Can be covered by the implementation of other gc recycling logic.

Request response class method

  • request After the request method is called, the EasySwoole\Http\Request object will be returned. This object comes with all the data requested by the user, for example:
    1. function index()
    2. {
    3. $request = $this->request();
    4. $request->getRequestParam();//Get post/get data, get overwrite post
    5. $request->getMethod();//Get request method (post/get/)
    6. $request->getCookieParams();//Get the cookie parameter
    7. }

::: warning More request related can view [request object] (request.md) :::

  • response
    The response method will return EasySwoole\Http\Response to respond to the client with data, for example:
    1. function index()
    2. {
    3. $response = $this->response();
    4. $response->withStatus(200);//Set response status code, must be set
    5. $response->setCookie('name','Alan',time()+86400,'/');//Set a cookie
    6. $response->write('hello world');//Send a piece of data to the client (similar to the echo of the regular web mode)
    7. }

::: warning For more information about response, see [response object] (response.md) :::

  • writeJson
    The writeJson method directly encapsulates the set response status code, sets the response header, and the array is converted to json output.
    1. function index()
    2. {
    3. $this->writeJson(200,['xsk'=>'Alan'],'success');
    4. }
    Web page output:
    1. {"code":200,"result":{"xsk":"仙士可"},"msg":"success"}

Deserialization method

  • json Parse a json string with json_decode
  • xml Parse xml string using simpleml_load_string

session related

  • sessionDriver Set the session driver class, the default is EasySwoole\Http\Session\SessionDriver
  • session Return the session driver class to manage the session

Verify related

  • validate The validate method can directly call the validation of the EasySwoole\Validate\Validate object, returning the result of the validation success/failure, and implementing the code:
    1. protected function validate(Validate $validate)
    2. {
    3. return $validate->validate($this->request()->getRequestParam());
    4. }
    We can use this method to verify the data sent by the client:
    1. function index()
    2. {
    3. $validate = new Validate();
    4. $validate->addColumn('name','Name')->required()->lengthMax(50);
    5. // Restricted name is required and cannot be greater than 50 strings
    6. if (!$this->validate($validate)){
    7. $this->writeJson(400, [], $validate->getError()->__toString());
    8. return false;
    9. }
    10. $this->writeJson(200, [], 'success');
    11. }

::: warning More validate related can be found [verifier] (./validate.md) :::