Upgrade Guide 1.6

Please back up before upgrading and execute the following commands to upgrade

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

webman-framework 1.6.0 Release

New Features

Requires PHP>=8.0

Support for Getting Input via 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,
        ]);
    }
}

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

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

Parameters also support binding to 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 information, refer to Controller Parameter Binding

Controller Middleware Support

<?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 to 4xx Requests

Normally 404 requests do not go through any middleware. Starting from version 1.6.0, you can set middleware 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 Default Routes for Specific Apps/Controllers

// Disable default routes for main project, does not affect application plugins
Route::disableDefaultRoute();
// Disable routes for admin app in main project, does not affect application plugins
Route::disableDefaultRoute('', 'admin');
// Disable default routes for foo plugin, does not affect main project
Route::disableDefaultRoute('foo');
// Disable default routes for admin app in foo plugin, does not affect main project
Route::disableDefaultRoute('foo', 'admin');
// Disable default route for 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 headers Data

$request->get(); // Assume result is ['name' => 'tom', 'age' => 18]
$request->setGet(['name' => 'tom']);
$request->get(); // Final result is ['name' => 'tom']
// Similar for the following
$request->setPost();
$request->setHeaders();

More reference documentation

view() Supports Omitting Template Parameter 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 Config and File Changes

In the new version, the listen and other configurations in config/server.php have been moved to config/process.php. This only affects new projects created with composer create-project, not existing project upgrades.
In the new version, the process directory has been moved to app/process. This only affects new projects created with composer create-project, not existing project upgrades.

Upgrade Notes

If the project code was not very standardized before, there may be some compatibility issues. The main issues are as follows:

Check view() Function Usage

Check whether view() in the project to be upgraded has template parameters starting with /. If so, remove the leading /, for example

return view('/user/index');
// Change to
return view('user/index');

Check Custom Route Parameter Consistency

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

Note: Add Request type hint before the $request parameter.

Disable Composer Proxy

Composer mirror proxies such as Aliyun have delays, and the package versions in them may not be the latest, causing upgrade issues. Please run the following command to remove the composer proxy before upgrading

composer config -g --unset repos.packagist