我正在开发一个laravel 5项目,并且在html中使用Auth时遇到了一些问题。
首先,我将展示我的代码
routes.php文件
// Router for login
Route::get('/login', ['as' => 'login_path', 'uses' => 'LoginController@getLogin']);
Route::post('/login', ['as' => 'post_login_path', 'uses' => 'LoginController@postLogin']);
// Router for dashboard
Route::get('dashboard', 'DashboardController@index');
当你去mysite / login时,有一个登录表单 当你填写输入并发布它时,它将转到LoginController @ postLogin
的LoginController
public function getLogin()
{
return view("login.login");
}
public function postLogin(Request $request)
{
$gebruikersnaam = $request->gebruikersnaam;
$wachtwoord = $request->wachtwoord;
if (Auth::attempt(['gebruikersnaam' => $gebruikersnaam, 'password' => $wachtwoord]))
{
//return JsonHandler::createJsonResponse("Successvol ingelogd redirect...", "/dashboard", "success");
return redirect('/dashboard');
//return redirect('home');
}
else {
return "fail";
}
//dd(Request::input());
//return $wachtwoord;
}
用户模型
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
// For custom password field in user table
public function getAuthPassword()
{
return $this->wachtwoord;
}
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'gebruikersnaam',
'wachtwoord'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
//protected $hidden = ['gebruikersnaam', 'wachtwoord'];
}
当auth尝试为true时,它将转到仪表板。但我无法在视图中使用auth fascade。
信息中心视图
<p>Welcome {{Auth::user()->gebruikersnaam}}</p>
我会收到以下错误:
尝试获取非对象的属性(查看:C:\xampp\htdocs\laravel5\resources\views\dashboard\master\dashboard-master.blade.php
)
我如何在我的视图中使用身份验证? 这是记录
的正确方法吗?