Controller Controller

As the core component of the HTTP service, the controller serializes the entire life cycle of a request. By means of annotation, the code is more concise than the traditional Controller, and the user can pay more attention to the business logic.

Create controller

Mainly through the @Controller annotation implementation. The code can be placed anywhere, but for uniform standards, it is recommended to put it under app/Http/Controller

You can quickly create a new controller with swoftcli :

 php swoftcli.phar gen:http-ctrl user --prefix /users 

annotation

Controller

Http controller class annotation tag @Controller

  • Annotation class: Swoft\Http\Server\Annotation\Mapping\Controller
  • Scope: CLASS
  • Have attributes:
    • prefix specified route prefix

Usually only @Controller is not effective, it needs to work with the next @RequestMapping to work correctly.

use

  • Explicitly specify the route prefix: @Controller(prefix="/index") or @Controller("/index")
  • Implicitly specify the route prefix: @Controller() automatically resolves the name of the controller class by default and uses the small camel format.
 <?php declare(strict_types=1);

namespace App\Http\Controller;

use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;

/**
 * @Controller()
 */
class IndexController
{
    /**
     * @RequestMapping(route="index")
     */
    public function index(){}
}

// OR

/**
 * @Controller(prefix="/v1/index")
 */
class IndexController
{
    /**
     * @RequestMapping(route="index")
     */
    public function index(){}
} 

Start the server:

  • Implicitly specify the route prefix access URL: http://127.0.0.1:18306/index/index
  • Display the specified route prefix access URL: http://127.0.0.1:18306/v1/index/index

Description

A complete routing rule is implemented via the @Controller + @RequestMapping annotation, usually the former defines the prefix and the latter defines the suffix.

  • As follows, the access route is /v1/users/list ( /v1/users + list )
 /**
 * @Controller(prefix="/v1/users")
 */
class UsersController
{
    /**
     * @RequestMapping(route="list")
     */
    public function list(){}
} 

Visit the URL: http://127.0.0.1:18306/v1/users/list

When @Controller is empty

 /**
 * @Controller()
 */
class UsersController
{
    /**
     * @RequestMapping(route="list")
     */
    public function list(){}
} 

Visit the URL: http://127.0.0.1:18306/users/list

@Controller and @RequestMapping are used together. Detailed reference to @RequestMapping annotation: routing

note

In Swoft, don't follow the traditional fpm framework to inherit the member properties of the parent class controller and use it on other controllers. This is wrong.

Such as:

 /**
 * @Controller()
 */
class BaseController
{
    protected $num;
}

/**
 * @Controller(prefix="/v1/index")
 */
class IndexController extends BaseController
{
    /**
     * @RequestMapping(route="index")
     */
    public function index()
    {
        $this->num++;
        echo $this->num."\n";
    }
} 
/docs/2.x/en/http-server/controller.html
progress-bar