国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

關(guān)于路由配置中使用call_user_func和參數(shù)自動注入沖突問題

redsky

問題描述

實現(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();
    }

截圖報錯信息里報錯文件相關(guān)代碼

Too few arguments to function app\api\controller\student\StudentController::getListData()

操作系統(tǒng)及workerman/webman等框架組件具體版本

webman : 1.4.7
php-di: 6.3

1064 1 0
1個回答

walkor 打賞

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語句

  • redsky 2022-12-10

    改了通過:
    $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)中!

  • walkor 2022-12-10

    call_user_func屬于手動調(diào)用,手動調(diào)用需要傳完整參數(shù),無法自動注入,這個任何框架都一樣。
    還有webman現(xiàn)在支持復(fù)雜的路由匹配,包括模塊/控制器/方法這種,不需要手動設(shè)置路由。

  • redsky 2022-12-10

    老大,因為我有一些特殊需求,根據(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

  • walkor 2022-12-10

    控制器返回的數(shù)據(jù)必須時reposne對象,不能是其他對象

  • redsky 2022-12-10

    老大,我控制器返回的就是Response對象呀,目前我確實不清楚該怎么去修改代碼,才能使用,還請老大能指點迷津,謝謝!

  • walkor 2022-12-10

    return Container::get($class_name); 返回的是控制器實例,不是response實例。

年代過于久遠,無法發(fā)表回答
??