简体   繁体   中英

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

@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

EDIT 1: for the first comment, yes, here is my web.php file:

<?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.

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 . Before it returns the signin view you could do Auth::check() , and if that is true, then do the 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.

In your web.php , have this take the place of the / route:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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