日本語 | English
Mysqlの替わりにSQLiteを使う。
ちょっとしたユーザ認証でMysqlをたてるのもなぁ... という思いがあってSQLiteを選んだが、実装するにあたり色々調べたのでまとめる。
Sentinelインストール
公式ドキュメントを参考に Composer とかで Sentinel と Illuminate を適宜入れる。
ここでは細かいことは触れない。
# composer.json ~~~ 略 ~~~ "require": { "cartalyst/sentinel": "2.0.*", "illuminate/database": "~5.1", "illuminate/events": "^5.2", "illuminate/http": "~5.0" }, ~~~ 略 ~~~
$ php composer.phar install
DB接続先の設定
公式ドキュメント: https://cartalyst.com/manual/sentinel/2.0#configuration
を元に設定をSQLiteに変える。
// Import the necessary classes use Cartalyst\Sentinel\Native\Facades\Sentinel; use Illuminate\Database\Capsule\Manager as Capsule; // Include the composer autoload file require 'vendor/autoload.php'; // Setup a new Eloquent Capsule instance $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'sqlite', 'database' => '/path/to/database.sqlite', 'charset' => 'utf8' ]); $capsule->bootEloquent();
"driver" を "sqlite" にして、 "database" にはsqliteファイルのフルパスを書く。
ユーザ認証用テーブルの作成
他のフレームワークでMysqlならばmigrationがあるので、自動で必要なテーブルが生成できるが、SQLiteでは手動でテーブルを作る。
Mysql用のSchema: sentinel/mysql.sql at 2.0 · cartalyst/sentinel · GitHub
を元に、SQLite用に作成して実行。
認証機能の動作を確認
公式ドキュメントの通り関数を実行して動作することを確認する
// Register a new user Sentinel::register([ 'email' => 'test@example.com', 'password' => 'foobar', ]); // Authenticate $credentials = [ 'email' => 'test@example.com', 'password' => 'foobar', ]; echo Sentinel::authenticate($credentials);