如何使用 HttpException 修复 Laravel 的路由错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46350523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:41:24  来源:igfitidea点击:

How to fix Laravel's routing error with HttpException?

phplaravel

提问by Geoff

Am using zizaco entruest rbac and these are my web routes causing error

我正在使用 zizaco entruest rbac,这些是我的网络路由导致错误

Route::group(['prefix' => 'user-management', 'middleware' => ['permission:admin']], function() {

    Route::get('/users', function(){
        return "sds";
    });

});

When i try navigating to

当我尝试导航到

http://localhost:8000/user-management/users

am getting an error

出现错误

Symfony \ Component \ HttpKernel \ Exception \ HttpException
No message

Where could i be wrong

我哪里错了

I have commented all other routes and found this to be the culprit

我已经评论了所有其他路线,发现这是罪魁祸首

I have setup my rbac as explained

我已经按照说明设置了我的 rbac

https://github.com/Zizaco/entrust

采纳答案by Clean code

You need to put auth middlewareas well as it checks permission for an auth user within permission middleware. You can fix it by just putting auth middlewareand then try logging in, now you can navigate to usersurl.

您需要放置auth middleware并检查 .auth 用户的权限permission middleware。您可以通过放置auth middleware然后尝试登录来修复它,现在您可以导航到usersurl。

 Route::group(['prefix' => 'user-management', 'middleware' => ['auth', 'permission:admin']], function() {

    Route::get('/users', function(){
       return "sds";
    });

 });

回答by Cong Chen

You should create a 403.blade.phptemplate view under resources/views/errorsdirectory.
Your application's exception handler do not find the exception status code associated view, so it return the exception directly.

您应该403.blade.phpresources/views/errors目录下创建一个模板视图。
您的应用程序的异常处理程序找不到与异常状态代码关联的视图,因此它直接返回异常。

回答by Zulfiqar Tariq

apparently your code looks fine. I guess you are having some problem in your middleware its not allowing you to pass through. you have used permission:admin so i guess you should log in as admin and then it will be working fine.

显然你的代码看起来不错。我猜你的中间件有问题,它不允许你通过。您已经使用了权限:admin,所以我想您应该以管理员身份登录,然后它就会正常工作。

回答by Eduardo Stuart

To help you with this we need more information.

为了帮助您,我们需要更多信息。

  • Is this the only route in your application?
  • Did you authenticate the user before trying to access /user-manag... ?
  • What's inside your app/Http/Kernel.php file? Did you add permission middleware?
  • 这是您应用程序中的唯一途径吗?
  • 在尝试访问 /user-manag... 之前,您是否对用户进行了身份验证?
  • 你的 app/Http/Kernel.php 文件里面有什么?您是否添加了权限中间件?

Not sure what kind of http code are you receiving (is it a 403? or a 500?).

不确定您收到的哪种 http 代码(是 403还是 500?)。

Things you can do that might help:

您可以做的事情可能会有所帮助:

  1. Enable debug mode (change your .env (or config/app.php) DEBUG to "true")
  2. Check your laravel.log file (storage/logs/laravel.log)
  3. Install laravel-debugbar. This package can give you better insight about what's happening in your app. https://github.com/barryvdh/laravel-debugbar
  1. 启用调试模式(将您的 .env(或 config/app.php)DEBUG 更改为“true”)
  2. 检查您的 laravel.log 文件 (storage/logs/laravel.log)
  3. 安装 laravel-debugbar。这个包可以让你更好地了解你的应用程序中发生的事情。 https://github.com/barryvdh/laravel-debugbar

回答by Mark Walet

I've looked up the middleware. It says the following:

我查了一下中间件。它说如下:

if ($this->auth->guest() || !$request->user()->can($permissions)) {
    abort(403);
}

This means that it crashes without a message if you aren't authenticated or you cannot do something because you don't have permission.

这意味着如果您未通过身份验证或由于您没有权限而无法执行某些操作,它会在没有消息的情况下崩溃。

You could try to dd()something in here before the abort. Just to see if it reaches this. If it does check this page in the Laravel documentation about abort()and custom error pages: https://laravel.com/docs/5.5/errors#http-exceptions

你可以dd()在中止之前尝试在这里做点什么。只是看看它是否达到了这一点。如果它确实在 Laravel 文档中检查此页面abort()和自定义错误页面:https: //laravel.com/docs/5.5/errors#http-exceptions