我正在尝试创建一个Bolt扩展,以便能够通过REST端点登录。但我无法从请求中捕获值。
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
public function initialize()
{
$this->app->post("/rest/session", array($this, 'rest_login'))
->bind('rest_login');
}
public function rest_login(Request $request) {
// Get credentials
$username = $request->get('username');
$password = $request->get('password');
// Attempt login
$login = $this->app['users']->login($username, $password);
$response = $this->app->json(array('login' => $login));
return $response;
}
如果我在获得$username
和$password
之后返回,我可以看到它们都是NULL,即使它们已作为POST数据发送 - 我如何捕获这些值?
答案 0 :(得分:2)
这只是理解Silex中的请求/响应过程的问题。扩展中的initialize方法在请求周期开始之前运行,要访问它,您需要注册一个控制器,然后可以设置路由来处理请求。这是一个简单的例子。
// In Extension.php
public function initialize() {
$this->app->mount('/', new ExampleController());
}
// In ExampleController.php
class ExampleController implements ControllerProviderInterface
{
public function connect(Silex\Application $app)
{
$ctr = $app['controllers_factory'];
$ctr->post("/rest/session", array($this, 'rest_login'))
->bind('rest_login');
}
public function rest_login(Request $request) {
........
}
}
这应该指向正确的方向。