我是Laravel的新手。我正在使用Laravel 6开发一个医学应用程序。本地的一切都很好。现在我试着在服务器(Heroku(上测试我的应用程序。登录屏幕加载得很好,但当我插入用户名和密码时,应用程序会重新加载登录页面,什么也没发生。
我的数据库存储在db4free.net上,如果我转到网址myapp.herokuapp.com/register并注册一个新用户,新用户就会添加到数据库中,所以我认为这不是数据库连接问题。如果能帮助你理解正在发生的事情,我将添加一些代码:
web.hp:
<?php
use IlluminateSupportFacadesRoute;
/*
|--------------------------------------------------------------------------
| 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('auth.login');
});
Auth::routes();
Route::get('profile', 'ProfileController@index')->name('profile');
Route::put('profile-update', 'ProfileController@update')->name('profile.update');
Route::put('profile-update-password', 'ProfileController@updatePassword')->name('profile.update.password');
Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'admin', 'middleware'=>['auth','admin']], function ()
{
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
Route::resource('user', 'UserController');
Route::resource('dosimeter', 'DosimeterController');
Route::resource('dosimeterref', 'DosimeterRefController');
Route::resource('room', 'RoomController');
Route::put('password-update/{id}', 'UserController@updatePassword')->name('password.update');
Route::put('password-reset/{id}', 'UserController@resetPassword')->name('password.reset');
});
Route::group(['as'=>'mphysicist.','prefix'=>'mphysicist','namespace'=>'mphysicist', 'middleware'=>['auth','mphysicist']], function ()
{
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});
Route::group(['as'=>'professional.','prefix'=>'professional','namespace'=>'professional', 'middleware'=>['auth','professional']], function ()
{
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});
LoginController.php
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use AppProvidersRouteServiceProvider;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateSupportFacadesAuth;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
//protected $redirectTo = '/';
protected $redirectTo;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
if(Auth::check() && Auth::user()->role->id == 1)
{
$this->redirectTo = route('admin.dashboard');
}
elseif (Auth::check() && Auth::user()->role->id == 2)
{
$this->redirectTo = route('mphysicist.dashboard');
}
else
{
$this->redirectTo = route('professional.dashboard');
}
$this->middleware('guest')->except('logout');
}
public function dni(){
return 'dni';
}
}
AdminMiddleware.php
<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateSupportFacadesAuth;
class AdminMiddleware
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check() && Auth::user()->role->id == 1)
{
return $next($request);
}
else
{
return redirect()->route('login');
}
}
}
我发现了问题。我将登录更改为使用DNI(国家id文档(而不是电子邮件,但我忘记了覆盖登录控制器中的登录方法。我不明白为什么它在没有这个覆盖的情况下在本地工作。
如果有人帮忙,这是我的覆盖登录方法:
public function login(Request $request)
{
$this->validate($request, [
'dni' => 'required',
'password' => 'required',
]);
if(Auth::attempt(['dni' => $request->dni, 'password' => $request->password]))
{
if(Auth::check() && Auth::user()->role->id == 1)
{
return redirect()->route('admin.dashboard');
}
elseif (Auth::check() && Auth::user()->role->id == 2)
{
return redirect()->route('mphysicist.dashboard');
}
else
{
return redirect()->route('professional.dashboard');
}
}else{
return redirect()->route('login')
->with('error','DNI and Password are wrong.');
}
}