【发布时间】:2023-03-17 03:05:02
【问题描述】:
我正在学习 Laravel,遇到了这个错误。
RouteCollection.php 第 161 行中的 NotFoundHttpException:
是的,我已经在网上搜索了解决方案,但没有一个能解决我的问题 我在同一个项目中有其他正在运行的路线, 只有这个身份验证路由不起作用。
AuthenticateController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use JWTAuth;
use App\Http\Controllers\Controller;
use Tymon\JWTAuth\Exceptions\JWTException;
class AuthenticateController extends Controller{
public function __construct(){
$this->middleware('jwt.auth',['except'=>['authenticate']]);
}
public function index(){
return "Auth index";
}
public function authenticate(Request $request){
$credentials =$request->only('phonenumber','generatednexmotoken');
try{
//verify the credentials and create a token for the user_error
if($token=JWTAuth::attempt($credentials)){
return response()->json(['error'=>'invalid_credentials']);
}else{
return response()->json(compact('token'));
}
}catch(Tymon\JWTAuth\Exceptions\JWTException $e){
return response()->json(['error'=>'could not create toke']);
}
}
public function getAuthenticatedUser(){
try{
if(! $user = JWTAuth::parseToken()->authenticate()){
return responnse()->json(['user not found',404]);
}else{
return response()->json(compact('user'));
}
}catch(Tymon\JWTAuth\Exceptions\TokenExpiredException $e){
return response()->json(['token expired'],$e->getStatusCode());
}catch(Tymon\JWTAuth\Exceptions\TokenInvalidExceptions $e){
return response()->json(['invalid token'],$e->getStatusCode());
}catch(Tymon\JWTAuth\Exceptions\JWTException $e){
return response()->json(['token is absent lol'],$e-
>getStatusCode());
}
}
}
用户.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'phonenumber', 'generatednexmotoken',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'generatednexmotoken', 'jwttoken',
];
}
api.php
Route::group(['prefix' => 'v1'], function()
{
Route::post('authenticate', 'AuthenticateController@authenticate');
Route::get('authenticate/user',
'AuthenticateController@getAuthenticatedUser');
});
Route::get('/sms/send/{to}', function(\Nexmo\Client $nexmo, $to){
$message = $nexmo->message()->send([
'to' => $to,
'from' => '@leggetter',
'text' => 'Sending SMS from Laravel. Woohoo!'
]);
Log::info('sent message: ' . $message['message-id']);
});
注意:
我只是在使用我的本地机器(php artisan serve)
api/sms/send/{to} 工作正常。
但如果我要访问 api/v1/authenticate,它将在 RouteCollection.php 第 161 行错误中抛出 NotFoundHttpException。
谢谢
【问题讨论】:
-
访问api/v1/authenticate是什么意思?发布请求还是获取请求?
-
@piscator 我确实在 POSTMAN 中通过 POST 访问了它,仔细检查了整个链接,这都是我的错,我拼错了 authenticate to athenticate 谢谢您的回复。