当前,我正在从一个非命名空间的php文件中调用一个命名空间的函数。
我的功能定义在:
ReservationValidation.php
namespace App\Reservation;
use Slim\Http\{Request, Response};
use Swaggest\JsonSchema\Schema;
function validateReservationPatch(Request $request, Response $response, callable $next)
{
try {
$schema = Schema::import(__DIR__ . '/schemas/patch.json');
$schema->in(json_decode($request->getBody()));
} catch(Exception $e) {
return $response->withStatus(400);
}
return $next($request, $response);
}
我从以下位置调用它:
route.php
<?php
use App\Auth\Authenticate;
use function App\Reservation\validateReservationPatch;
$reservationPath = '/reservations';
$app->add(new Authenticate())->add(function ($request, $response, $next) {
validateReservationPatch($request, $response, $next);
})->patch($reservationPath, 'ReservationController:updateReservation');
我遇到了这个错误:
Slim Application Error
The application could not run because of the following error:
Details
Type: Error
Message: Call to undefined function
App\Reservation\validateReservationPatch()
File: E:\workspace\php-playground\residence-reservation-microservice\src\App\Reservation\routes.php
Line: 14
Trace
#0 [internal function]: Closure->{closure}(Object(Slim\Http\Request),
Object(Slim\Http\Response), Object(Closure))
#1 E:\workspace\php-playground\residence-reservation-
microservice\vendor\slim\slim\Slim\DeferredCallable.php(43):
call_user_func_array(Object(Closure), Array)
#2 [internal function]: Slim\DeferredCallable-
>__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response),
Object(Closure))
#3 E:\workspace\php-playground\residence-reservation-
microservice\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70):
call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request),
Object(Slim\Http\Response), Object(Closure))
#4 E:\workspace\php-playground\residence-reservation-
microservice\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App-
>Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 E:\workspace\php-playground\residence-reservation-
microservice\vendor\slim\slim\Slim\App.php(406): Slim\App-
>callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 E:\workspace\php-playground\residence-reservation-
microservice\vendor\slim\slim\Slim\App.php(314): Slim\App-
>process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 E:\workspace\php-playground\residence-reservation-
microservice\public\index.php(33): Slim\App->run()
#8 {main}
我可以调用命名空间的类,但不能调用该函数。有想法吗?