1.6 Upgrade Guide

Please ensure to back up before upgrading, and run the following commands to upgrade

composer config -g --unset repos.packagist
composer require -W workerman/webman-framework ^v1.6.11

webman-framework Released Version 1.6.0

New Features

Requires PHP>=8.0

Support for retrieving input through controller parameters

<?php
namespace app\controller;
use support\Response;

class UserController
{
    public function create(string $name, int $age, float $balance, bool $vip, array $extension): Response
    {
        return json([
            'name' => $name,
            'age' => $age,
            'balance' => $balance,
            'vip' => $vip,
            'extension' => $extension,
        ]);
    }
}

Accessing /user/create?name=tom&age=18&balance=100.5&vip=1&extension[foo]=bar yields the result

{
  "name": "tom",
  "age": 18,
  "balance": 100.5,
  "vip": true,
  "extension": {
    "foo": "bar"
  }
}

At the same time, parameters support binding classes including models, for example

<?php
namespace app\controller;
use app\model\User;
class UserController
{
    public function create(User $user): int
    {
        $user->save();
        return $user->id;
    }
}

For more reference controller parameter binding

Support for middleware in controllers

<?php
namespace app\controller;
use app\middleware\MiddlewareA;
use app\middleware\MiddlewareB;
use support\Request;
class IndexController
{
    protected $middleware = [
        MiddlewareA::class,
        MiddlewareB::class,
    ];
    public function index(Request $request): string
    {
        return 'hello';
    }
}

Support for Route::fallback()->middleware(...); to add middleware for 4xx requests

Under normal circumstances, 404 requests do not go through any middleware. Starting from version 1.6.0, middleware can be set for 4xx requests.

Route::fallback(function(){
    return json(['code' => 404, 'msg' => '404 not found']);
})->middleware([
    app\middleware\MiddlewareA::class,
    app\middleware\MiddlewareB::class,
]);

Support for Route::disableDefaultRoute() to disable the default routes of specific applications and controllers

// Disable the default route of the main project, does not affect application plugins
Route::disableDefaultRoute();
// Disable the routes of the admin application of the main project, does not affect application plugins
Route::disableDefaultRoute('', 'admin');
// Disable the default routes of the foo plugin, does not affect the main project
Route::disableDefaultRoute('foo');
// Disable the default routes of the admin application of the foo plugin, does not affect the main project
Route::disableDefaultRoute('foo', 'admin');
// Disable the default route of the controller [\app\controller\IndexController::class, 'index']
Route::disableDefaultRoute([\app\controller\IndexController::class, 'index']);

More reference documentation

Support for $request->setGet(), $request->setPost(), $request->setHeader() to override get, post, and headers data

$request->get(); // Suppose to get ['name' => 'tom', 'age' => 18]
$request->setGet(['name' => 'tom']);
$request->get(); // Finally get ['name' => 'tom']
// The following is similar
$request->setPost();
$request->setHeaders();

More reference documentation

view() supports omitting template parameters and absolute paths

<?php
namespace app\controller;
use support\Request;
class UserController
{
    public function hello(Request $request)
    {
        // Equivalent to return view('user/hello', ['name' => 'webman']);
        // Equivalent to return view('/app/view/user/hello', ['name' => 'webman']);
        return view(['name' => 'webman']);
    }
}

Other configuration and file changes

In the new version, configurations such as listen in config/server.php have been moved to config/process.php, which only affects new projects created with composer create-project; upgrading old projects will not be affected.
In the new version, the process directory has been moved to app/process, which only affects new projects created with composer create-project; upgrading old projects will not be affected.

Upgrade Notes

If the previous project code is not very standardized, there may be some compatibility issues, the main issues are as follows:

Check the usage of the view() function

Check whether the view() in the project to be upgraded has template parameters that start with /, if so, remove the leading /, for example

return view('/user/index');
// Needs to be changed to
return view('user/index');

Check for consistency in custom routing parameters

Route::any('/user/{name}', function (Request $request, $myname) {
   return response($myname);
});
// Needs to be changed to
Route::any('/user/{name}', function (Request $request, $name) {
   return response($name);
});

Note that the $request parameter needs to have a Request type hint added before it.

Disable composer proxy

Composer mirror proxies such as Alibaba Cloud may have delay issues, and the versions of the packages inside may not be the latest, leading to upgrade problems. Please run the following command to delete the composer proxy, and then upgrade.

composer config -g --unset repos.packagist