實現(xiàn)目標:路由自動匹配:模塊/控制器/方法,主要參考了以下兩個地址
https://github.com/walkor/webman/issues/54
http://wtbis.cn/q/6685
目前碰到問題,在路由中匹配完成后,通過call_user_func來調(diào)用方法,而在方法中我原先使用了自動注入,現(xiàn)在兩者沖突了。
路由匹配實現(xiàn)
Route::group('/{module}', function () {
Route::group("/{controller}", function () {
Route::post("/{action}", function ($request, $module, $controller, $action) {
return toRoute($request, $module, $controller, $action, 'create');
});
Route::delete("/{action}", function ($request, $module, $controller, $action) {
return toRoute($request, $module, $controller, $action, 'delete');
});
Route::get("/{action}", function ($request, $module, $controller, $action) {
return toRoute($request, $module, $controller, $action, 'get');
});
Route::put("/{action}", function ($request, $module, $controller, $action) {
return toRoute($request, $module, $controller, $action, 'update');
});
});
});
function toRoute($request, $module, $controller, $action, $method = 'get') {
$class_name = 'app\\api\\controller\\'.$module.'\\'.ucfirst($controller).'Controller';
if(!class_exists($class_name)){
return json(['code'=>404,'msg'=>config('app.debug')?'控制器:'.$class_name.' 不存在':'請求資源不存在']);
}
$action = $method . ucfirst($action);
if (!method_exists($class_name, $action)) {
return json(['code'=>404,'msg'=>config('app.debug')?'控制器方法:'.$action.' 不存在':'請求資源不存在']);
}
$controller = new $class_name;
$request->controller = $class_name;
return call_user_func([$controller, $action], $request);
}
方法部分代碼
public function getListData ($request, Student $student)
{
return $this->success();
}
Too few arguments to function app\api\controller\student\StudentController::getListData()
webman : 1.4.7
php-di: 6.3
http://wtbis.cn/doc/webman/di.html#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E6%B3%A8%E5%85%A5
手冊有講,必須是由框架或者php-di創(chuàng)建的實例才能完成依賴自動注入,手動new的實例無法完成依賴自動注入,如需注入,需要使用support\Container接口替換new語句
改了通過:
$controller = Container::get($class_name);
$request->controller = $class_name;
$request->action = $action;
return call_user_func([$controller, $action], $request);
結(jié)果還是報錯:
Too few arguments to function app\api\controller\ding\ApiController::getListData(), 1 passed
是我的用法錯了嗎?請老大指點,從thinkphp轉(zhuǎn)webman,還在適應(yīng)中!
call_user_func屬于手動調(diào)用,手動調(diào)用需要傳完整參數(shù),無法自動注入,這個任何框架都一樣。
還有webman現(xiàn)在支持復(fù)雜的路由匹配,包括模塊/控制器/方法這種,不需要手動設(shè)置路由。
老大,因為我有一些特殊需求,根據(jù)用戶的請求類型,然后在方法前自動帶上請求類型,比如訪問路由為:system/api/menu,請求方法為get,那么該路由定位的控制器應(yīng)該是:app/api/controller/system/Apicontroller,方法是:getMenu。內(nèi)置的路由應(yīng)該無法滿足我的需求,所以需要自己做下處理。
現(xiàn)在我改為:
function toRoute($request, $module, $controller, $action, $method = 'get') {
$class_name = "app\api\controller\".$module."\".ucfirst($controller)."Controller";
if(!class_exists($class_name)){
return json(['code'=>404,'msg'=>config('app.debug')?'控制器:'.$class_name.' 不存在':'請求資源不存在']);
}
$action = $method . ucfirst($action);
if (!method_exists($class_name, $action)) {
return json(['code'=>404,'msg'=>config('app.debug')?'控制器方法:'.$action.' 不存在':'請求資源不存在']);
}
return Container::get($class_name);
}
但是又出現(xiàn)了新的問題:
Object of class app\api\controller\***** could not be converted to string
謝謝老大,目前我用:http://wtbis.cn/q/9059;這里的方法經(jīng)過改寫,已實現(xiàn)我的需求。再次感謝,祝愿webman workerman 越來越好!