我正在尝试在with()
上发送数据,但在视图中,登录页面会刷新,但不会显示任何内容。我几乎已经尝试了所有内容,我已经阅读了几乎所有方式尝试过的许多问题。
我的控制器,if条件正在运行。
public function authenticate(Request $request)
{
$validator = Validator::make(Input::get(),
[
'password' => 'required',
'username' => 'required'
]
);
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator->errors())
->with('message', 'Invalid Username or Password.')
->with('form', 'login')
->withInput(\Input::except('password'));
}
$user = array(
'name' => Input::get('username'),
'password' => Input::get('password')
);
if (Auth::attempt($user)) {
return redirect()->intended('dashboard');
} else {
$request->session()->put('error_message', 'Incorrect email or password');
$request->session()->put('form', 'login');
return redirect()
->back();
}
}
我的观点是:
<form class="login-form" method="post" style="{{{ Session::get('form', 'login') == 'login' ? 'display:block' : 'display:none;' }}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<h3 class="form-title">Login to your account</h3>
<div class="alert alert-danger {{{ (Session::has('message') && Session::get('form', 'login') == 'login') ? '' : 'display-hide' }}}">
<button class="close" data-close="alert"></button>
<span>
{!! Session::has('message') ? Session::get('message') : 'Please correct your fields.' !!}
</span>
<span>
{!! session()->has('error_message') ? session('error_message') : 'Please correct your fields.' !!}
</span>
</div>
<div class='form-group {{ $errors->has("username") ? "has-error":"" }}'>
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">@Username</label>
<div class="input-icon">
<i class="fa fa-envelope"></i>
{!! Form::text('username', '', ['class' => 'form-control placeholder-no-fix', 'placeholder' => 'Username']) !!}
<span class="help-block"> {{ $errors->first("username") }} </span>
</div>
</div>
<div class='form-group {{ $errors->has("password") ? "has-error":"" }}'>
<label class="control-label visible-ie8 visible-ie9">Password</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
{!! Form::password('password', ['class' => 'form-control placeholder-no-fix', 'placeholder' => 'Password']) !!}
<span class="help-block"> {{ $errors->first("password") }} </span>
</div>
</div>
<div class="form-actions">
<!--<label class="checkbox">
<input type="checkbox" name="remember" value="1"/> Remember me </label>-->
<button type="submit" class="btn red pull-right">
Login <i class="m-icon-swapright m-icon-white"></i>
</button>
</div>
<br/>
<div class="forget-password">
<h4>Forgot your password ?</h4>
<p>
<a href="javascript:;" id="forget-password">Reset</a> your account login info.
</p>
</div>
</form>
Routes.php有这些路线:
Route::group(['middleware' => 'guest'], function ()
{
// login routes
Route::get('/login', ['as' => 'login', 'uses' => "CommonController@showLogin"]);
Route::post('/login', ['uses' => "CommonController@authenticate"]);
Route::post('/reset', "Auth\PasswordController@postEmail");
Route::get('/password/reset/{token}', 'CommonController@showReset');
Route::post('/password/reset', 'Auth\PasswordController@postReset');
});
Route::get('/welcome', 'HomeController@index');
Route::group(['middleware' => ['auth']], function ()
{
// COMMONS
Route::get('/', 'CommonController@index');
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'CommonController@routeDashboard']);
Route::get('/logout', ['as' => 'logout', 'uses' => "CommonController@logout"]);
Route::group(['as' => 'admin::', 'middleware' => 'role:admin'], function ()
{
// USERS
Route::group(['as' => 'users::', 'prefix' => 'users'], function ()
{
Route::get('/', ['as' => 'manage', 'uses' => 'AdminController@showManageUsers']);
Route::post('handle', ['as' => 'handle', 'uses' => 'AdminController@handleUsers']);
Route::get('new', ['as' => 'new', 'uses' => 'AdminController@newUser']);
Route::post('new', ['uses' => 'AdminController@newUserSave']);
Route::get('edit-basic/{id}', ['as' => 'edit_basic', 'uses' => 'AdminController@editUserBasic']);
Route::post('edit-basic/{id}', ['uses' => 'AdminController@editUserBasicSave']);
Route::get('edit-historical/{id}', ['as' => 'edit_historical', 'uses' => 'AdminController@editUserHistorical']);
Route::post('edit-historical/{id}', ['uses' => 'AdminController@editUserHistoricalSave']);
Route::post('validate-username', ['as' => 'validate_username', 'uses' => 'AdminController@validateUsername']);
Route::post('validate-email/{id?}', ['as' => 'validate_email', 'uses' => 'AdminController@validateEmail']);
});
});
});
错误的用户名和密码消息不会显示。
答案 0 :(得分:3)
尝试使用$request->session()->put()
添加消息:
$request->session()->put('error_message', 'Incorrect email or password');
$request->session()->put('form', 'login');
获取会话数据:
{!! session()->get('error_message', 'Please correct your fields.') !!}
或者
{!! session()->has('error_message') ? session('error_message') : 'Please correct your fields.' !!}
另外,检查您的会话是否正常运行。丢失会话数据的事情很少,例如manually adding web
middleware in 5.2.27及更高版本。
答案 1 :(得分:1)
使用
return Redirect::back()->withErrors(['msg', 'The Message']);
并在视图中调用此
@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
答案 2 :(得分:0)
将web添加到中间件组
Route::group(['middleware' => ['auth', 'web']], function ()
进入下面的代码
Route::group(['middleware' => ['auth']], function ()
{
// COMMONS
Route::get('/', 'CommonController@index');
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'CommonController@routeDashboard']);
Route::get('/logout', ['as' => 'logout', 'uses' => "CommonController@logout"]);
Route::group(['as' => 'admin::', 'middleware' => 'role:admin'], function ()
{
// USERS
Route::group(['as' => 'users::', 'prefix' => 'users'], function ()
{
Route::get('/', ['as' => 'manage', 'uses' => 'AdminController@showManageUsers']);
Route::post('handle', ['as' => 'handle', 'uses' => 'AdminController@handleUsers']);
Route::get('new', ['as' => 'new', 'uses' => 'AdminController@newUser']);
Route::post('new', ['uses' => 'AdminController@newUserSave']);
Route::get('edit-basic/{id}', ['as' => 'edit_basic', 'uses' => 'AdminController@editUserBasic']);
Route::post('edit-basic/{id}', ['uses' => 'AdminController@editUserBasicSave']);
Route::get('edit-historical/{id}', ['as' => 'edit_historical', 'uses' => 'AdminController@editUserHistorical']);
Route::post('edit-historical/{id}', ['uses' => 'AdminController@editUserHistoricalSave']);
Route::post('validate-username', ['as' => 'validate_username', 'uses' => 'AdminController@validateUsername']);
Route::post('validate-email/{id?}', ['as' => 'validate_email', 'uses' => 'AdminController@validateEmail']);
});
});
});