我正在尝试将表单中的用户名和密码传递给控制器,检查该用户是否存在(用户数据库表),然后重定向到经过身份验证的页面,其中只有登录的用户才能看到他们的用户名。
在我的routes.php
:
$app->post('/', ['uses' => 'AuthenticationController@login']);
$app->group(['middleware' => 'auth'], function ($app) {
$app->post('/tickets', ['uses' => 'TicketsController@getTickets']);
});
然后我的AuthenticationController.php
登录功能:
public function login(Request $request) {
$credentials = $request->only(['username','password']);
$user = App\User::find($credentials);
if ($user) {
// I'm assuming you do a redirect() here but am unsure how with Auth
} else {
return 'not logged in';
}
}
此时,我不知道我需要在TicketsController.php中放入getTickets
函数。
任何知道如何做到这一点的人,请告诉我如何继续下去。
最终,让tickets.blade.php
视图拥有{{ user->username }}
并显示谁登录的用户名会很棒。如果用户没有登录并访问此视图,则不会显示,因为它们未经过身份验证。
编辑:目前,我收到此错误:
AuthenticationController.php中的FatalErrorException:Class 找不到'App \ Http \ Controllers \ App \ User'
在所有功能开始之前,我确实在AuthenticationController.php
中包含了这两行:
use App\User;
use Auth;
也许这就是我将$credentials
传递给find函数的方式,但这并不能解决身份验证方面的问题。