缺少模板休息api CakePHP 3.x.

时间:2015-11-03 03:41:45

标签: php cakephp cakephp-3.x

我使用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']
  ]);
}

0 个答案:

没有答案