ふたりはララベル (Laravel PHP Framework)

PHPフレームワークのLaravelの体験記を書いていきます。こんなタイトルのブログですが萌え系アニメは一秒たりとも観たことがありません。

Laravel専用のユーザ認証Confideを使ってみる

Webアプリケーション・フレームワーク用の認証パッケージはSentryが有名なんだけど、Laravel専用の認証パッケージConfide(カンファイド)というのもある。今回はConfideを使ってみる。
Zizaco/confide · GitHub

インストール

まずはConfideをインストールする。

C:\xampp\laravel>php composer.phar update
Loading composer repositories with package information
Updating dependencies (including require-dev)

  - Installing zizaco/confide (2.0.0b4)
    Downloading: 100%

サービスプロパイダにConfideを追加する

「config/app.php」の「$providers」の末尾に「'Zizaco\Confide\ConfideServiceProvider'」を追加する。

'providers' => array(

  'Illuminate\Foundation\Providers\ArtisanServiceProvider',
  'Illuminate\Auth\AuthServiceProvider',
  'Illuminate\Cache\CacheServiceProvider',
  'Illuminate\Foundation\Providers\CommandCreatorServiceProvider',
  'Illuminate\Session\CommandsServiceProvider',
  'Illuminate\Foundation\Providers\ComposerServiceProvider',
  'Illuminate\Routing\ControllerServiceProvider',
  'Illuminate\Cookie\CookieServiceProvider',
  'Illuminate\Database\DatabaseServiceProvider',
  'Illuminate\Encryption\EncryptionServiceProvider',
  'Illuminate\Filesystem\FilesystemServiceProvider',
  'Illuminate\Hashing\HashServiceProvider',
  'Illuminate\Html\HtmlServiceProvider',
  'Illuminate\Foundation\Providers\KeyGeneratorServiceProvider',
  'Illuminate\Log\LogServiceProvider',
  'Illuminate\Mail\MailServiceProvider',
  'Illuminate\Foundation\Providers\MaintenanceServiceProvider',
  'Illuminate\Database\MigrationServiceProvider',
  'Illuminate\Foundation\Providers\OptimizeServiceProvider',
  'Illuminate\Pagination\PaginationServiceProvider',
  'Illuminate\Foundation\Providers\PublisherServiceProvider',
  'Illuminate\Queue\QueueServiceProvider',
  'Illuminate\Redis\RedisServiceProvider',
  'Illuminate\Auth\Reminders\ReminderServiceProvider',
  'Illuminate\Foundation\Providers\RouteListServiceProvider',
  'Illuminate\Database\SeedServiceProvider',
  'Illuminate\Foundation\Providers\ServerServiceProvider',
  'Illuminate\Session\SessionServiceProvider',
  'Illuminate\Foundation\Providers\TinkerServiceProvider',
  'Illuminate\Translation\TranslationServiceProvider',
  'Illuminate\Validation\ValidationServiceProvider',
  'Illuminate\View\ViewServiceProvider',
  'Illuminate\Workbench\WorkbenchServiceProvider',
  'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
  'Zizaco\Confide\ConfideServiceProvider',

),

Confide用のテーブルを作る

artisanコマンドを使ってConfide用のテーブルを作る。

C:\xampp\laravel>php artisan confide:migration

Table name: users
An migration that creates the users table will be created in app/database/migrations directory

Proceed with the migration creation? [Yes|no]Yes

Creating migration...
Migration successfully created!


C:\xampp\laravel>php artisan migrate
Migrated: 2013_11_08_195510_confide_setup_users_table

Confide用のコントローラを作る

artisanコマンドを使ってConfide用のコントローラを作る。

C:\xampp\laravel>php artisan confide:controller

Controller name: UserController
An authentication controller template with the name UserController.php will be created in app/controllers directory and will NOT overwrite any  file.

Proceed with the controller creation? [Yes|no]Yes

Creating UserController...
UserController.php Successfully created!

route.phpにConfide用のルーティングを追加書きする

route.phpにConfide用のルーティングを追加する必要があるんだけど、artisanコマンドを使えばそれも自動的にやってくれる。至れり尽くせりだ。

C:\xampp\laravel>php artisan confide:routes

Routes file: app/routes.php
The default Confide routes (to use with the Controller template) will be appended to your routes.php file.

Proceed with the append? [Yes|no]Yes

Appending routes...
// Confide routes
Route::get( 'user/create',                 'UserController@create');
Route::post('user',                        'UserController@store');
Route::get( 'user/login',                  'UserController@login');
Route::post('user/login',                  'UserController@do_login');
Route::get( 'user/confirm/{code}',         'UserController@confirm');
Route::get( 'user/forgot_password',        'UserController@forgot_password');
Route::post('user/forgot_password',        'UserController@do_forgot_password');
Route::get( 'user/reset_password/{token}', 'UserController@reset_password');
Route::post('user/reset_password',         'UserController@do_reset_password');
Route::get( 'user/logout',                 'UserController@logout');

app/routes.php Patched successfully!

Composerのオートローダに登録する

これはよくわからないけど、Confideのマニュアルには「忘れるなよ」って書いてある。

C:\xampp\laravel>composer dump-autoload
Generating autoload files

Confideを試す

「http://あなたのサイト名/user/create」にアクセスして、ユーザ追加画面が出たら成功。
f:id:laravel:20131113000734p:plain

filter.phpにConfideを追加する

サイトにアクセスするとログイン画面が出るようにするには、filter.phpに追加書きが必要になる。

app/filter.phpのオリジナルのRoute::filter('auth', function())は以下なんだけど、

Route::filter('auth', function()
{
	if (Auth::guest()) return Redirect::guest('login');
});

これを下記のように変更する。

Route::filter('auth', function()
{
    if ( Auth::guest() ) // If the user is not logged in
    {
        return Redirect::guest('user/login');
    }
});

route.phpに認証を追加する

最後はapp/route.phpに「Route::when('', 'auth');」を追加すれば完成。これで必ずログイン画面に遷移する。

Route::when('', 'auth');