New PHP 8+ Features in Webman: Controller Parameter Binding, Middleware, and Routing Enhancements

This guide explains Webman's latest additions for PHP 8+, covering controller method parameter injection, middleware support, fallback route middleware, disabling default routes, request data overriding, and simplified view rendering, along with upgrade considerations.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
New PHP 8+ Features in Webman: Controller Parameter Binding, Middleware, and Routing Enhancements

New Features

Requirements:

PHP>=8.0

Controller Parameter Binding

You can now receive request inputs directly as method arguments:

<?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 it via

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

to receive:

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

Model binding is also supported:

<?php
namespace app\controller;
use app\model\User;

class UserController {
    public function create(User $user): int {
        $user->save();
        return $user->id;
    }
}

See the controller parameter binding docs at

https://www.workerman.net/doc/webman/controller.html#%E6%8E%A7%E5%88%B6%E5%99%A8%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A

.

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';
    }
}

Fallback Route Middleware for 4xx Requests

From version 1.6.0, 4xx responses can run middleware:

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

Disable Default Routes

// Disable main project default routes (plugins unaffected)
Route::disableDefaultRoute();
// Disable admin app routes in main project
Route::disableDefaultRoute('', 'admin');
// Disable default routes of plugin "foo"
Route::disableDefaultRoute('foo');
// Disable admin routes of plugin "foo"
Route::disableDefaultRoute('foo', 'admin');
// Disable default route of a specific controller action
Route::disableDefaultRoute([\app\controller\IndexController::class, 'index']);

More details at

https://www.workerman.net/doc/webman/route.html#%E7%A6%81%E7%94%A8%E9%BB%98%E8%AE%A4%E8%B7%AF%E7%94%B1

.

Request Data Overriding

$request->get(); // returns ['name'=>'tom','age'=>18]
$request->setGet(['name'=>'tom']);
$request->get(); // now returns ['name'=>'tom']
// Similar methods exist for POST and headers
$request->setPost();
$request->setHeaders();

Reference:

https://www.workerman.net/doc/webman/request.html#%E9%87%8D%E5%86%99%E5%8F%82%E6%95%B0

.

View Rendering Simplifications

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

class UserController {
    public function hello(Request $request) {
        // Equivalent to return view('user/hello', ['name'=>'webman']);
        // Also works with absolute path view('/app/view/user/hello', ...)
        return view(['name'=>'webman']);
    }
}

Upgrade Considerations

If your existing codebase is not strictly following conventions, you may encounter compatibility issues:

Check view() Calls

Ensure no leading slash is used in template names. Change:

return view('/user/index'); // wrong
// to
return view('user/index'); // correct

Verify Custom Route Parameters

Make sure route placeholders match the variable names used in callbacks. Example adjustment:

Route::any('/user/{name}', function (Request $request, $myname) {
    return response($myname);
});
// should become
Route::any('/user/{name}', function (Request $request, $name) {
    return response($name);
});
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

middlewareWebmanPHP8ControllerBindingUpgradeGuide
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.