我正在开发一个工作驱动器/服务器(M :)并使用总共3个数据库。其中两个数据库是SQL Server,另一个是MySQL。我已经实现了一个Laravel Multi Auth包,可以在这里找到:https://github.com/ollieread/multiauth。即使文档很容易理解,我仍然无法验证用户并将用户登录到我的应用程序中。请帮助。
应用/配置/ app.php:
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
// 'Illuminate\Auth\AuthServiceProvider',
'Illuminate\Cache\CacheServiceProvider',
'Illuminate\Session\CommandsServiceProvider',
'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
'Illuminate\Routing\ControllerServiceProvider',
'Illuminate\Cookie\CookieServiceProvider',
'Illuminate\Database\DatabaseServiceProvider',
'Illuminate\Encryption\EncryptionServiceProvider',
'Illuminate\Filesystem\FilesystemServiceProvider',
'Illuminate\Hashing\HashServiceProvider',
'Illuminate\Html\HtmlServiceProvider',
'Illuminate\Log\LogServiceProvider',
'Illuminate\Mail\MailServiceProvider',
'Illuminate\Database\MigrationServiceProvider',
'Illuminate\Pagination\PaginationServiceProvider',
'Illuminate\Queue\QueueServiceProvider',
'Illuminate\Redis\RedisServiceProvider',
'Illuminate\Remote\RemoteServiceProvider',
// 'Illuminate\Auth\Reminders\ReminderServiceProvider',
'Illuminate\Database\SeedServiceProvider',
'Illuminate\Session\SessionServiceProvider',
'Illuminate\Translation\TranslationServiceProvider',
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
'Way\Generators\GeneratorsServiceProvider',
"Ollieread\Multiauth\Reminders\ReminderServiceProvider",
"Ollieread\Multiauth\MultiauthServiceProvider"
),
'aliases' => array(
'App' => 'Illuminate\Support\Facades\App',
'Artisan' => 'Illuminate\Support\Facades\Artisan',
'Auth' => 'Illuminate\Support\Facades\Auth',
'Blade' => 'Illuminate\Support\Facades\Blade',
'Cache' => 'Illuminate\Support\Facades\Cache',
'ClassLoader' => 'Illuminate\Support\ClassLoader',
'Config' => 'Illuminate\Support\Facades\Config',
'Controller' => 'Illuminate\Routing\Controller',
'Cookie' => 'Illuminate\Support\Facades\Cookie',
'Crypt' => 'Illuminate\Support\Facades\Crypt',
'DB' => 'Illuminate\Support\Facades\DB',
'Eloquent' => 'Illuminate\Database\Eloquent\Model',
'Event' => 'Illuminate\Support\Facades\Event',
'File' => 'Illuminate\Support\Facades\File',
'Form' => 'Illuminate\Support\Facades\Form',
'Hash' => 'Illuminate\Support\Facades\Hash',
'HTML' => 'Illuminate\Support\Facades\HTML',
'Input' => 'Illuminate\Support\Facades\Input',
'Lang' => 'Illuminate\Support\Facades\Lang',
'Log' => 'Illuminate\Support\Facades\Log',
'Mail' => 'Illuminate\Support\Facades\Mail',
'Paginator' => 'Illuminate\Support\Facades\Paginator',
'Password' => 'Illuminate\Support\Facades\Password',
'Queue' => 'Illuminate\Support\Facades\Queue',
'Redirect' => 'Illuminate\Support\Facades\Redirect',
'Redis' => 'Illuminate\Support\Facades\Redis',
'Request' => 'Illuminate\Support\Facades\Request',
'Response' => 'Illuminate\Support\Facades\Response',
'Route' => 'Illuminate\Support\Facades\Route',
'Schema' => 'Illuminate\Support\Facades\Schema',
'Seeder' => 'Illuminate\Database\Seeder',
'Session' => 'Illuminate\Support\Facades\Session',
'SoftDeletingTrait' => 'Illuminate\Database\Eloquent\SoftDeletingTrait',
'SSH' => 'Illuminate\Support\Facades\SSH',
'Str' => 'Illuminate\Support\Str',
'URL' => 'Illuminate\Support\Facades\URL',
'Validator' => 'Illuminate\Support\Facades\Validator',
'View' => 'Illuminate\Support\Facades\View',
),
应用/配置/ auth.php:
'multi' => array(
'user' => array(
'driver' => 'eloquent',
'model' => 'User'
),
'client' => array(
'driver' => 'database',
'table' => 'clients'
)
),
应用/配置/ database.php中:
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'blah',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'some.ip.address',
'database' => 'bleh',
'username' => 'some username',
'password' => '',
'prefix' => '',
'MultipleActiveResultSets' => true,
'ConnectionPooling' => true,
'ReturnDatesAsStrings' => true
),
'sqlsrv2' => array(
'driver' => 'sqlsrv',
'host' => 'some.ip.address',
'database' => 'blooh',
'username' => 'some username2',
'password' => '',
'prefix' => '',
'MultipleActiveResultSets' => true,
'ConnectionPooling' => true,
'ReturnDatesAsStrings' => false
)
),
控制器:
public function postAdminLogin()
{
$email = Input::get('email');
$password = Input::get('password');
if (Auth::user()->attempt(array('email' => $email, 'password' => $password))) {
$user = Auth::user();
return Redirect::action('UsersController@index');
} else {
// login failed, go back to the login screen
Session::flash('errorMessage', 'Wrong Company ID or Password');
return Redirect::back()->withInput();
}
}
模型
<?php
use Ollieread\Multiauth\MultiauthServiceProvider;
use Ollieread\Multiauth\Reminders\ReminderServiceProvider;
class User extends Eloquent{
protected $connection = 'mysql';
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $fillable = [];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
public static $rules = array(
'username' => 'max:100',
'email' => 'required|max:100',
'password' => 'required|min:3'
);
protected $hidden = array('password', 'remember_token');
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
}