我非常喜欢Laravel(这很重要)))
我的应用程序应该使用现有数据库中的数据(只读)。所以我将两个数据库连接到我的应用程序,一个用于Laravel,第二个用于读取数据。我需要为第二个数据库中存在的用户进行身份验证,并将其保存在“本机”Laravel'用户'表中。例如。:
在我的DEV中,DB以下列方式连接
在.env
:
...
DB_CONNECTION=sqlite
DB_DATABASE=/path/to/app/db.sqlite
DB_CONNECTION_LEGACY=sqlite
DB_DATABASE_LEGACY=/path/to/app/legacy.sqlite
...
在config/database.php
:
...
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'sqlite_legacy' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE_LEGACY', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
...
请帮我。我被卡住了。
对不起英语不好,这不是我的母语。
完成。
为我的旧数据库创建模型:
php artisan make:model OldUsers
在app\OldUsers.php
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class eboard_user extends Model
{
/**
* Connection for old 'users' table
*
* @var string
*/
protected $connection = 'sqlite_legacy';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The storage format of the model's date columns.
*
* @var string
*/
protected $dateFormat = 'U';
}
在app/Http/Controllers/Auth/LoginController.php
:
<?php
namespace App\Http\Controllers\Auth;
use App\OldUsers;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Hash;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
} else {
$checkedUser = OldUsers::whereEmail($request->input('email'))->wherePass(md5($request->input('password')))->whereStatus('2')->first(); //Selecting user from old DB via needed criteria
if ($checkedUser !== null) { //If old user exists:
$newUser = new User; //Create Laravel native user
$newUser->name = $checkedUser->name;
$newUser->email = $checkedUser->email;
$newUser->password = Hash::make($request->input('password'));
$newUser->save();
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
}
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
}