slim 3 将附加参数传递给控制器
slim 3 pass additional param to controller
这是我的原始代码:
$app->post('/api/user', function (Request $request, Response $response, array $args) use ($tokenAuth) {
$tokenAuth->parseHeaders(); // validate token
//...do stuff with $tokenAuth data
$response->getBody()->write(json_encode(...)));
return $response;
});
请注意 use ($tokenAuth)
这允许我在方法中使用此对象。
如何以相同的方式使用下面的方法?
$app->post('/api/user', \UserController::class . ':add');
当路由到达 class UserController:
时工作正常
class UserController {
public function add($request, $response, $args) { ...}
}
如何将 $tokenAuth
传递给它?
看看你的原始代码,你可以使用$tokenAuth
(并且有效),这意味着你之前定义了全局。
因此,在您的 add
方法中,只需将其声明为全局变量:global $tokenAuth;
然后使用它;
public function add($request, $response, $args) {
global $tokenAuth;
$tokenAuth->parseHeaders();
...
}
您可以将 $tokenAuth
和 UserController
注册到依赖管理器,并通过依赖注入机制将 $tokenAuth
传递给 UserController
。例如:
<?php
namespace Your\Own\NameSpace;
use Your\Own\NameSpace\TokenAuth;
class UserController
{
private $tokenAuth;
public function __construct(TokenAuth $tokenAuth)
{
$this->tokenAuth = $tokenAuth;
}
public function add($request, $response, $args)
{
$this->tokenAuth->parseHeaders();
...
}
}
在您的依赖注册代码中(假设使用 Pimple):
<?php
use Your\Own\NameSpace\TokenAuth;
use Your\Own\NameSpace\UserController;
$container[TokenAuth::class] = function ($c) {
return new TokenAuth();
};
$container[UserController::class] = function ($c) {
$tokenAuth = $c->get(TokenAuth::class);
return new UserController($tokenAuth);
};
我最终使用中间件在路由上进行身份验证:
$mw = function($request, $response, $next){
$tokenAuth = new \TokenAuth();
$tokenAuth->parseHeaders();
$response = $next($request, $response);
return $response;
};
$app = new \Slim\App();
$app->post('/api/user', \UserController::class . ':add')->add($mw);
$app->run();
这是我的原始代码:
$app->post('/api/user', function (Request $request, Response $response, array $args) use ($tokenAuth) {
$tokenAuth->parseHeaders(); // validate token
//...do stuff with $tokenAuth data
$response->getBody()->write(json_encode(...)));
return $response;
});
请注意 use ($tokenAuth)
这允许我在方法中使用此对象。
如何以相同的方式使用下面的方法?
$app->post('/api/user', \UserController::class . ':add');
当路由到达 class UserController:
时工作正常class UserController {
public function add($request, $response, $args) { ...}
}
如何将 $tokenAuth
传递给它?
看看你的原始代码,你可以使用$tokenAuth
(并且有效),这意味着你之前定义了全局。
因此,在您的 add
方法中,只需将其声明为全局变量:global $tokenAuth;
然后使用它;
public function add($request, $response, $args) {
global $tokenAuth;
$tokenAuth->parseHeaders();
...
}
您可以将 $tokenAuth
和 UserController
注册到依赖管理器,并通过依赖注入机制将 $tokenAuth
传递给 UserController
。例如:
<?php
namespace Your\Own\NameSpace;
use Your\Own\NameSpace\TokenAuth;
class UserController
{
private $tokenAuth;
public function __construct(TokenAuth $tokenAuth)
{
$this->tokenAuth = $tokenAuth;
}
public function add($request, $response, $args)
{
$this->tokenAuth->parseHeaders();
...
}
}
在您的依赖注册代码中(假设使用 Pimple):
<?php
use Your\Own\NameSpace\TokenAuth;
use Your\Own\NameSpace\UserController;
$container[TokenAuth::class] = function ($c) {
return new TokenAuth();
};
$container[UserController::class] = function ($c) {
$tokenAuth = $c->get(TokenAuth::class);
return new UserController($tokenAuth);
};
我最终使用中间件在路由上进行身份验证:
$mw = function($request, $response, $next){
$tokenAuth = new \TokenAuth();
$tokenAuth->parseHeaders();
$response = $next($request, $response);
return $response;
};
$app = new \Slim\App();
$app->post('/api/user', \UserController::class . ':add')->add($mw);
$app->run();