slim framework

孙承弼
2023-12-01

middleware include controller and model

中间件引入控制器和数据模型

$app->add(function ($request, $response, $next) {
     $request_url = $request->getUri()->getPath();
    $request_arr = explode('/',$request_url);
    $request_last_key = count($request_arr) - 1;
    if($request_arr[0] != 'swagger.json' && $request_arr[$request_last_key ] != ""){
        $is_controller = include_once('src/controllers/'.$request_arr[$request_last_key ].'.php');

        if(!$is_controller){
            $error = new stdClass();
            $error->code = 1001;
            $error->message = "The controller doesn't exist";
            $response = $response->withJson($error,404, JSON_PRETTY_PRINT);
            return $response;
        }

        
        $is_model = include_once('src/models/'.$request_arr[$request_last_key ].'.php');
        if(!$is_model){
            $error = new stdClass();
            $error->code = 1002;
            $error->message = "The model doesn't exist";
            $response = $response->withJson($error,404, JSON_PRETTY_PRINT);
            return $response;
        }
    }
	$response = $next($request, $response);

	return $response;
});

 

 

 类似资料:

相关阅读

相关文章

相关问答