假设您已经正确创建了一个包含所需列的“用户”表。您可以使用 Laravel 内置的身份验证系统来检查用户是否已登录。在您的情况下,听起来如果用户未登录,您想继续创建一个随机用户并使用该用户登录。这是一个注释代码示例,应该可以帮助您。
就会话而言,如果您使用 Laravel 的内置身份验证......您根本不必担心会话,Laravel 会为您处理一切。
编辑
这一切都将在控制器中完成。
<?php
// First ask Laravel if the user is logged in.
if (Auth::guest())
{
// If not, let's create a new user, save it, then log in with that newly created user.
$newUser = new User;
$newUser->username = str_random();
$newUser->password = Hash::make(str_random());
$newUser->save();
// This login() function allows us to just login someone in without any hassle.
// If you were collecting and checking login credentials, that's when you would use attempt()
Auth::login($newUser)
}
else
{
// He is already logged in. You can then access his user information like so...
$user = Auth::user();
$user->username; // Would return his username.
}
// At this point, the user is defintely logged in one way or another. So we can then send the view as normal.
return View::make('members.home');