简体   繁体   English

laravel重定向到用户登录时的路由

[英]laravel redirect to route when user is logged in

i am a beginner in laravel, i am trying to redirect to another route if the user is logged in, the signup and login are working perfectly and are not a problem, but when i try to do 我是laravel的初学者,如果用户已登录,我正尝试重定向到另一条路线,注册和登录工作正常,这不是问题,但是当我尝试执行操作时

@if(Auth::check())
    {{
        redirect()->route('news')
    }}
@endif

the redirect script gets output on the screen like this: 重定向脚本将在屏幕上获得如下输出:

HTTP/1.0 302 Found Cache-Control: no-cache, private Location: http://localhost/red-sec/public/news <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="1;url=http://localhost/red-sec/public/news" /> <title>Redirecting to http://localhost/red-sec/public/news</title> </head> <body> Redirecting to <a href="http://localhost/red-sec/public/news">http://localhost/red-sec/public/news</a>. </body> </html>

Please excuse me if i did a rookie mistake i am extremely new to laravel, and the news route is set up correctly and is working 如果我犯了一个菜鸟错误,请原谅我对laravel非常陌生,并且新闻路线设置正确且正在运行

EDIT 1: for the first comment, yes, here is my web.php file: 编辑1:对于第一个评论,是的,这是我的web.php文件:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
})->name('home');

Route::post('/signup', [
    'uses' => 'UserController@postSignUp',
    'as' => 'signup'
]);

Route::post('/signin', [
    'uses' => 'UserController@postSignIn',
    'as' => 'signin'
]);

Route::get('/news', [
    'uses' => 'userController@getNews',
    'as' => 'news',
    'middleware' => 'auth'
]);

You shouldn't try to (and can't) redirect in views. 您不应该尝试(也不能)重定向视图。 Views should be ONLY used to display data, not to do business logic. 视图只能用于显示数据,不能用于业务逻辑。

Because you are not using controller to do any logic (you return view directly from router), you can do something like this: 因为您没有使用控制器执行任何逻辑(您直接从路由器返回视图),所以可以执行以下操作:

Route::get('/', function () {
    if(Auth::check()) {
        return redirect()->route('news');
    }

    return view('welcome');
})->name('home');

Text displayed in your view is actually a HTTP response. 视图中显示的文本实际上是HTTP响应。

so I'm assuming you want to see if they're already logged in, and if they are you want to redirect them away from the login page? 因此,我假设您想查看它们是否已经登录,以及是否要从登录页面重定向它们? You could accomplish that in a Route::get('/signin') method on UserController . 您可以在UserController上的Route::get('/signin')方法中完成此操作。 Before it returns the signin view you could do Auth::check() , and if that is true, then do the redirect()->route('news') 在返回登录视图之前,您可以执行Auth::check() ,如果为true,则执行redirect()->route('news')

You should note, however, that Laravel ships with a ton of authentication scaffolding already in place, which you can read about here. 但是,您应该注意,Laravel附带了很多身份验证支架, 您可以在此处阅读有关信息。

In your web.php , have this take the place of the / route: 在您的web.php ,用它代替/路由:

Route::get('/', function() {
  if (Auth::check()) {
     return redirect()->route('news');
  }
  else {
    return view('welcome');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM