laravel5.4 前后台未登陆,跳转到各自的页面

举报
lxw1844912514 发表于 2022/03/27 22:54:23 2022/03/27
【摘要】 https://www.imooc.com/wenda/detail/378208?t=266634 laravel我做了前后台登陆,后台未登录跳转到前台登陆页面了。 我想让后台未登入跳转到后台登陆页面,前台未登陆跳转到前台登陆页面。 config\auth.php 添加guards中的admin和provide...

https://www.imooc.com/wenda/detail/378208?t=266634

laravel我做了前后台登陆,后台未登录跳转到前台登陆页面了。 我想让后台未登入跳转到后台登陆页面,前台未登陆跳转到前台登陆页面。

config\auth.php 
添加guards中的admin和providers中的admins


    
  1. <?php
  2. 'defaults' => [
  3. 'guard' => 'web',
  4. 'passwords' => 'users',
  5. ],
  6. 'guards' => [
  7. 'web' => [
  8. 'driver' => 'session',
  9. 'provider' => 'users',
  10. ],
  11. 'api' => [
  12. 'driver' => 'token',
  13. 'provider' => 'users',
  14. ],
  15. 'admin' => [
  16. 'driver' => 'session',
  17. 'provider' => 'admins',
  18. ],
  19. ],
  20. 'providers' => [
  21. 'users' => [
  22. 'driver' => 'eloquent',
  23. 'model' => App\User::class,
  24. ],
  25. 'admins' => [
  26. 'driver' => 'eloquent',
  27. 'model' => App\AdminUser::class,
  28. ],
  29. ],
  30. 'passwords' => [
  31. 'users' => [
  32. 'provider' => 'users',
  33. 'table' => 'password_resets',
  34. 'expire' => 60,
  35. ],
  36. ],

 路由


    
  1. //登陆页面
  2. Route::get('/login', "\App\Http\Controllers\LoginController@index")->name('login');
  3. //登陆行为
  4. Route::post('/login', "\App\Http\Controllers\LoginController@login");
  5. Route::group(['middleware' => 'auth:web'],function (){
  6. Route::get('/posts', '\App\Http\Controllers\PostController@index');
  7. }
  8. //后台
  9. Route::group(['prefix' => 'admin'], function() {
  10. Route::get('/login', '\App\Admin\Controllers\LoginController@index');
  11. Route::post('/login', '\App\Admin\Controllers\LoginController@login');
  12. Route::get('/logout', '\App\Admin\Controllers\LoginController@logout');
  13. Route::group(['middleware' => 'auth:admin'],function (){
  14. Route::get('/home', '\App\Admin\Controllers\HomeController@index');
  15. });
  16. });

 遇到的页面跳转问题

解答:

需要在 App\Exceptions\Handler.php 文件修改


    
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Auth\AuthenticationException;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. class Handler extends ExceptionHandler
  7. {
  8. /**
  9. * A list of the exception types that should not be reported.
  10. *
  11. * @var array
  12. */
  13. protected $dontReport = [
  14. \Illuminate\Auth\AuthenticationException::class,
  15. \Illuminate\Auth\Access\AuthorizationException::class,
  16. \Symfony\Component\HttpKernel\Exception\HttpException::class,
  17. \Illuminate\Database\Eloquent\ModelNotFoundException::class,
  18. \Illuminate\Session\TokenMismatchException::class,
  19. \Illuminate\Validation\ValidationException::class,
  20. ];
  21. /**
  22. * Report or log an exception.
  23. *
  24. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  25. *
  26. * @param \Exception $exception
  27. * @return void
  28. */
  29. public function report(Exception $exception)
  30. {
  31. parent::report($exception);
  32. }
  33. /**
  34. * Render an exception into an HTTP response.
  35. *
  36. * @param \Illuminate\Http\Request $request
  37. * @param \Exception $exception
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function render($request, Exception $exception)
  41. {
  42. return parent::render($request, $exception);
  43. }
  44. /**
  45. * Convert an authentication exception into an unauthenticated response.
  46. *
  47. * @param \Illuminate\Http\Request $request
  48. * @param \Illuminate\Auth\AuthenticationException $exception
  49. * @return \Illuminate\Http\Response
  50. */
  51. protected function unauthenticated($request, AuthenticationException $exception)
  52. {
  53. if ($request->expectsJson()) {
  54. return response()->json(['error' => 'Unauthenticated.'], 401);
  55. }
  56. if (in_array('admin', $exception->guards())) {
  57. return redirect()->guest('/admin/login');
  58. }
  59. #return redirect()->guest(route('login'));
    return redirect()->guest(route('/')); #亲测可行
} }
   

 解答2:

后端路由  加上 

Route::get('/login', '\App\Admin\Controllers\LoginController@index')->name('login');

     

 

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/100028459

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。