我正在尝试在Helpers的帮助下验证我的用户 为此,我在应用程序目录中创建了Helper文件夹。将以下代码行添加到composer.json
"files": [
"app/Helpers/UserHelper.php"
],
在App \ Provider目录中创建HelperServiceProvider.php,并在其中使用以下代码。
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
}
之后我在app.php中添加别名以及添加这样的提供
//this is an alias
'UserHelper' => App\Helpers\UserHelper::class,
//this is an provider
App\Providers\HelperServiceProvider::class,
我的用户模型是
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $table='users';
protected $fillable =['username', 'password', 'firstname', 'lastname', 'email', 'phone', 'groupname', 'about', 'image'];
public static $login = [
'username' => 'required|',
'email' => 'required|',
'password' => 'required'
];
}
这是我的UserHelper
<?php namespace App\Helpers;
use Illuminate\Support\Facades\Auth;
class UserHelper {
public static function processLogin($inputs){
if(Auth::attempt($inputs)){
return TRUE;
} else {
return FALSE;
}
}
}
这是我的登录功能
<?php
namespace App\Http\Controllers;
use App\User;
use Input;
use Illuminate\Support\Facades\Validator as Validator;
use App\Helpers\UserHelper;
class LoginController extends Controller
{
public function login() {
$inputs = Input::except('_token');
$validator = Validator::make($inputs, User::$login);
if($validator->fails()){
print_r($validator->errors()->first());
} else {
$respones = \UserHelper::processLogin($inputs);
if($respones){
return 'loginView';
} else {
return 'not a user of our DB';
}
}
}
}
我还更新了我的作曲家,登录到应用程序后出现错误,我正在搜索最近5小时的任何帮助吗?
Reards
答案 0 :(得分:1)
In your code you are extending the class User extends Model but when you are using auth functionality in laravel you need to extend the auth rather than model..
Keep Illuminate\Foundation\Auth\User and extends the model like this...
class User extends Authenticatable{
//code here
}