我使用CakePHP 3.x构建了一个rest api但是当我使用Postman 3.1.1进行测试时,500内部服务器出错了:
找不到UsersController :: token()的视图。)
在档案routes.php
中:
Router::prefix('api', function ($routes) {
$routes->extensions(['json']);
$routes->resources('Users');
$routes->resources('Organizations');
$routes->resources('AccountRegistrations');
$routes->fallbacks('InflectedRoute');
});
在控制器中:
public function token() {
$user = $this->Auth->identify();
if (!$user) {
$message ='Invalid username or password';
}
$this->set([
'success' => true,
'data' => [
'token' => $token = \JWT::encode([
'id' => $user['id'],
'exp' => time() + 604800
],
Security::salt())
],
'_serialize' => ['success', 'data']
]);
}