routing

Unlike traditional PHP frameworks, Swoft does not use configuration files to configure routing, but uses annotations. In Swoft we can use the @RequestMapping annotation to quickly add routes.

Router configuration

 // at file: vendor/swoft/http-server/src/AutoLoader.php
    'httpRouter'      => [
        'name'            => 'swoft-http-router',
        // config
        'ignoreLastSlash' => true,
        'tmpCacheNumber'  => 500,
        // 'handleMethodNotAllowed' => false
    ], 

Routing configuration instructions

  • ignoreLastSlash bool Default: true ignores the last URI path /

If set to false not ignored, /home and /home/ will be two different routes

  • tmpCacheNumber int default: 500 dynamic routing cache number.

After the dynamic parameter route is matched, it will be cached. The next time the same route will match the hit faster.

  • handleMethodNotAllowed bool Default: false Whether to process MethodNotAllowed

In order to speed up the matching, the default method does not match and also throws a Route not found error. This option can be turned on if there is a special need, and a Method Not Allowed error will be thrown when it is turned on.

Routing annotation

RequestMapping annotation

Settable properties:

  • route routing rule path
  • method request method (GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD)
  • params can be used to add regular matching restrictions to the path variable.

Note: Try to write only one @RequestMapping annotation on each method to avoid confusion.

Use example

  • Typically, a complete routing path is equal to the Controller prefix + @RequestMapping the route
  • In particular , when the route on your RequestMapping.route starts with / , the complete route is it, ie the prefix is no longer added to it.

Simple to use

Use the method to add a RequestMapping annotation to the controller method

  • Display the specified route suffix: @RequestMapping("index") or @RequestMapping(route="index")
  • Implicitly specifying the route suffix: Use @RequestMapping() default the resolution method name to the suffix
 /**
 * @Controller()
 */
class UserController
{
    /**
     * @RequestMapping()
     */
    public function index()
    {}

    /**
     * @RequestMapping("index")
     */
    public function index()
    {}

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

The same effect as above, the route bound to the index method is /user/index , and the allowed request methods are the default GET and POST .

Binding route path parameter

  • Specify the routing parameter: @RequestMapping(route="index/{name}") , you can use $name as the method parameter directly in the Action method.
  • When the route parameter is wrapped by [] , the URL path pass parameter is optional, and may or may not be available.
 <?php declare(strict_types=1);

namespace App\Http\Controller;

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

/**
 * @Controller()
 */
class UserController
{
    /**
     * @RequestMapping(route="/index/{name}")
     */
    public function index(string $name)
    {}

    /**
     * @RequestMapping(route="/index/{name}")
     */
    public function index(string $name)
    {}
} 

Set the method parameter

If you want to set the HTTP request mode that allows the request controller. You can use the method to call the @RequestMapping annotation in the controller to configure the method parameter, which can be one or more of GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD.

  • Qualify HTTP method: @RequestMapping(method={RequestMethod::GET}) specifies the HTTP method supported by the route. The default is to support GET and POST

For example, method={RequestMethod::POST,RequestMethod::PUT} sets the route to support POST and PUT

 <?php declare(strict_types=1);

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

/**
 * @Controller()
 */
class UserController
{
    /**
     * @RequestMapping(method=RequestMethod::GET)
     */
    public function index()
    {}

    /**
     * @RequestMapping(method={RequestMethod::GET,RequestMethod::POST})
     */
    public function index()
    {}

    /**
     * @RequestMapping("index", method=RequestMethod::PUT)
     */
    public function index()
    {}
} 

Use attention

Please remember to introduce related annotation classes

  • Swoft\Http\Server\Annotation\Mapping\RequestMapping
  • Swoft\Http\Server\Annotation\Mapping\RequestMethod

Get matching results

You can get the result of the route matching in the middleware or action.

 [$status, $path, $route] = $request->getAttribute(Request::ROUTER_ATTRIBUTE); 
/docs/2.x/en/http-server/route.html
progress-bar