Auth::login() 在 Laravel 8 中不起作用
Auth::login() is not working in Laravel 8
Auth::login()
无效。在做这个项目之前,我做了另一个然后就成功了。
这是用户模型
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
use HasFactory;
}
这是我的登录控制器:
function loggedIn(Request $request){
echo $name = $request['name'];
$password = $request['password'];
$user = User::where([
["name", $name],
["password", $password],
])->get()->first();
if ($user != ''){
Auth::login($user);
return redirect()->route('welcome');
}else{
return redirect()->back()->with(['error'=>'Ismingiz tasdiqlanmadi!']);
}
}
控制器重定向欢迎路由,但欢迎路由再次将其重定向到登录页面。
路由 welcome 有中间件 auth:
Route::get('/', function () {
return view('welcome');
})->name('welcome')->middleware('auth');
如果你有解决办法,请告诉我!
正如 onlie Thomas 在评论中写道:
try to create a smaller example for yourself and see where things
start to no longer work. I would suggest stop echo-ing like in
loggedIn because this disrupts some functions in Laravel. for example
bacause: >Remember that header() must be called before any actual
output is sent
我的错误,我在header()
函数之前写了echo
;
Auth::login()
无效。在做这个项目之前,我做了另一个然后就成功了。
这是用户模型
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
use HasFactory;
}
这是我的登录控制器:
function loggedIn(Request $request){
echo $name = $request['name'];
$password = $request['password'];
$user = User::where([
["name", $name],
["password", $password],
])->get()->first();
if ($user != ''){
Auth::login($user);
return redirect()->route('welcome');
}else{
return redirect()->back()->with(['error'=>'Ismingiz tasdiqlanmadi!']);
}
}
控制器重定向欢迎路由,但欢迎路由再次将其重定向到登录页面。 路由 welcome 有中间件 auth:
Route::get('/', function () {
return view('welcome');
})->name('welcome')->middleware('auth');
如果你有解决办法,请告诉我!
正如 onlie Thomas 在评论中写道:
try to create a smaller example for yourself and see where things start to no longer work. I would suggest stop echo-ing like in loggedIn because this disrupts some functions in Laravel. for example bacause: >Remember that header() must be called before any actual output is sent
我的错误,我在header()
函数之前写了echo
;