使用记住令牌的 Laravel 登录/用户问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25859115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Login / User Issue with Remember Token
提问by merlinpatt
I just started working with a year old Laravel project for my fellowship and a major bug inhibiting progress is in the way. Any help would be greatly appreciated. Story below.
我刚刚开始使用一个已有一年历史的 Laravel 项目作为我的奖学金,并且阻碍了进展的主要错误。任何帮助将不胜感激。下面的故事。
Once Laravel is up and running and I try to log in, I get this error:
一旦 Laravel 启动并运行,我尝试登录,我收到此错误:
Class User contains 3 abstract methods and must therefore be declared abstract or
implement the remaining methods
(Illuminate\Auth\UserInterface::getRememberToken,
Illuminate\Auth\UserInterface::setRememberToken,
Illuminate\Auth\UserInterface::getRememberTokenName)
I believed this could be fixed via http://laravel.com/docs/upgrade#upgrade-4.1.26and so I added the following lines to my User as suggested:
我相信这可以通过http://laravel.com/docs/upgrade#upgrade-4.1.26 解决,所以我按照建议向我的用户添加了以下几行:
public function getRememberToken(){ return $this->remember_token; }
public function setRememberToken($value){ $this->remember_token = $value; }
public function getRememberTokenName(){ return 'remember_token'; }
This got rid of the error but I still cannot log in. I'll use the correct information but it seems to redirect me back to the login page. Upon further research, I discovered that I may need to have a [b]remember_token[/b] field in my database. I attempted to add this and remigrate by adding:
这消除了错误,但我仍然无法登录。我将使用正确的信息,但它似乎将我重定向回登录页面。经过进一步研究,我发现我的数据库中可能需要有一个 [b]remember_token[/b] 字段。我尝试添加此内容并通过添加以下内容重新迁移:
$table->rememberToken();
[edit 2014-09-16, 07:05: added parens. they were in my code but forgotten in the post]
But that produced this stacktrace:
但这产生了这个堆栈跟踪:
PHP Fatal error: Call to undefined method Illuminate\Database\Schema\Blueprint::rememberToken() in /vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php on line 24
PHP Stack trace:
PHP 1. {main}() /vagrant/www/vfamatching.dev/artisan:0
PHP 2. Symfony\Component\Console\Application->run() /vagrant/www/vfamatching.dev/artisan:59
PHP 3. Symfony\Component\Console\Application->doRun() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Application.php:121
PHP 4. Symfony\Component\Console\Application->doRunCommand() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Application.php:191
PHP 5. Illuminate\Console\Command->run() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Application.php:893
PHP 6. Symfony\Component\Console\Command\Command->run() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Console/Command.php:96
PHP 7. Illuminate\Console\Command->execute() /vagrant/www/vfamatching.dev/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:244
PHP 8. Illuminate\Database\Console\Migrations\MigrateCommand->fire() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Console/Command.php:108
PHP 9. Illuminate\Database\Migrations\Migrator->run() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:67
PHP 10. Illuminate\Database\Migrations\Migrator->runMigrationList() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:84
PHP 11. Illuminate\Database\Migrations\Migrator->runUp() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:113
PHP 12. CreateUsersTable->up() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:137
PHP 13. Illuminate\Support\Facades\Schema::create() /vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php:25
PHP 14. Illuminate\Support\Facades\Facade::__callStatic() /vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php:25
PHP 15. Illuminate\Database\Schema\Builder->create() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:209
PHP 16. CreateUsersTable->{closure:/vagrant/www/vfamatching.dev/app/database/migrations/2013_11_01_033240_create_users_table.php:15-25}() /vagrant/www/vfamatching.dev/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:91
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Call to undefined method Illuminate\Database\Schema\Blueprint::rememberToken()","file":"\/vagrant\/www\/vfamatching.dev\/app\/database\/migrations\/2013_11_01_033240_create_users_table.php","line":24}}vagrant@devbox:/vagrant/www/
[edit 2014-09-16, 07:05: tried watcher's answer] After watcher's answer last night, I have tried using their solution by replacing
[edit 2014-09-16, 07:05: 尝试观察者的回答] 昨晚观察者的回答后,我尝试通过替换使用他们的解决方案
$table->rememberToken(); with
$table->string('remember_token', 100);
The migration no longer throws an error but the login still hangs. Also, I should point out that Laravel recommends using the rememberToken
syntax
迁移不再抛出错误,但登录仍然挂起。另外,我应该指出 Laravel 建议使用rememberToken
语法
My login route
我的登录路径
Route::get('login', array('as' => 'login', function () { return View::make('login'); }))->before('guest');
Route::post('login', 'UsersController@login');
and controller method
和控制器方法
public function login()
{
$user = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($user)) {
Auth::user()->login();
Auth::user()->lastLogin = Carbon::now();
Auth::user()->save();
if (Session::has('returnUrl'))
{
$intendedDestination = Session::get('returnUrl');
Session::forget('returnUrl');
return Redirect::to($intendedDestination)
->with('flash_success', 'You are successfully logged in.');
}
return Redirect::to('/')
->with('flash_success', 'You are successfully logged in.');
}
// authentication failure! lets go back to the login page
return Redirect::route('login')
->with('flash_error', 'Your username/password combination was incorrect.')
->withInput();
}
回答by Jeff Lambert
'remember_token'
is just a varchar
field and should be created like any other in a migration:
'remember_token'
只是一个varchar
字段,应该像迁移中的任何其他字段一样创建:
$table->string('remember_token', 100)->nullable();
Update
更新
In response to your comment, you are correct, the schema builder documentation does allow the rememberToken
method, it's interesting that the upgrade guidemakes no mention of it. It is only available in Laravel v4.2 and up and, as of this writing, is nothing more than an alias for the code above:
为了回应您的评论,您是对的,架构构建器文档确实允许使用该rememberToken
方法,有趣的是升级指南没有提及它。它仅在 Laravel v4.2 及更高版本中可用,在撰写本文时,它只不过是上述代码的别名:
// File: /vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php
/**
* Adds the `remember_token` column to the table.
*
* @return \Illuminate\Support\Fluent
*/
public function rememberToken()
{
return $this->string('remember_token', 100)->nullable();
}
If you are on 4.2 or up, I would use the rememberToken
function rather than the string
function to define the column, just in case the function definition above changes in any future versions.
如果您使用的是 4.2 或更高版本,我将使用rememberToken
函数而不是string
函数来定义列,以防万一上述函数定义在任何未来版本中发生变化。
As for your login attempt 'hanging', please update your question with the related controller method and I can maybe help you further.
至于您的登录尝试“挂起”,请使用相关的控制器方法更新您的问题,我也许可以进一步帮助您。
回答by martinstoeckli
Maybe it is just a small mistake, rememberToken is a method and should be called like this:
也许只是一个小错误,rememberToken 是一个方法,应该这样调用:
$table->rememberToken();