【问题标题】:Laravel 5.4 (with JWT) - NotFoundHttpException in RouteCollection.php line 161Laravel 5.4(使用 JWT) - RouteCollection.php 第 161 行中的 NotFoundHttpException
【发布时间】: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']);
 });

注意

  1. 我只是在使用我的本地机器(php artisan serve)

  2. api/sms/send/{to} 工作正常。

  3. 但如果我要访问 api/v1/authenticate,它将在 RouteCollection.php 第 161 行错误中抛出 NotFoundHttpException。

谢谢

【问题讨论】:

  • 访问api/v1/authenticate是什么意思?发布请求还是获取请求?
  • @piscator 我确实在 POSTMAN 中通过 POST 访问了它,仔细检查了整个链接,这都是我的错,我拼错了 authenticate to athenticate 谢谢您的回复。

标签: php laravel jwt


【解决方案1】:

您如何访问api/v1/authenticate.? (浏览器或其他像邮递员或前端)。您的代码似乎正确,但如果您尝试通过 GET 方法访问 api/v1/authenticate api,则可能会出错。你能提供你是如何访问它的吗?

【讨论】:

  • 感谢您指出@Vaibhavraj,当我检查我的邮递员时,我的身份验证拼写错误:D
猜你喜欢
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2016-07-03
  • 2016-10-13
  • 1970-01-01
  • 2018-07-29
相关资源
最近更新 更多