プロミディア合同会社の斉藤です。
今回はLaravel5.5でログイン機能を実装する方法を解説します。
ただ、実装と言ってもコマンドひとつでできてしまいますので、動かすだけならやることはほとんどありません。
簡単すぎて拍子抜けすると思いますのでご覧ください。
makeコマンドの実行
プロジェクト直下で下記コマンドを実行します。
php artisan make:auth
マイグレーションを実行し、usersテーブル、password_resetsテーブルを作成します。
php artisan migrate
これだけで、もうログイン機能は動作します。
ソースコード解説
ソースコードはどのようになったのか見てみましょう。
ルーティング
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); |
まず「Auth::routes();」が増えていますね。
これはログイン関連のルーティングを束ねた記述です。
実際のルーティングはvendor以下のRouter.phpに書かれています。
vendor/laravel/framework/src/Illuminate/Routing/Router.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/** * Register the typical authentication routes for an application. * * @param array $options * @return void */ public function auth(array $options = []) { // Authentication Routes... $this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); $this->post('logout', 'Auth\LoginController@logout')->name('logout'); // Registration Routes... if ($options['register'] ?? true) { $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); $this->post('register', 'Auth\RegisterController@register'); } // Password Reset Routes... if ($options['reset'] ?? true) { $this->resetPassword(); } // Email Verification Routes... if ($options['verify'] ?? false) { $this->emailVerification(); } } /** * Register the typical reset password routes for an application. * * @return void */ public function resetPassword() { $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request'); $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset'); $this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update'); } /** * Register the typical email verification routes for an application. * * @return void */ public function emailVerification() { $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice'); $this->get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify'); $this->get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend'); } |
コントローラ
app/Http/Controllers/HomeController.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('home'); } } |
make Authのコマンドによりサンプル画面として、/homeも追加されました。
HomeControllerにはコンストラクタでミドルウェアとしてauthが設定されています。
これにより、ログインチェックがかかりますので、ログインせずに/homeにアクセスしたら、いったんログイン画面に飛ばされることになります。
画面表示
サンプルのログイン画面はこんな感じ。
会員登録がまだであれば、Registerから会員登録もできます。
画面デザインは「resources/views/auth」以下に格納されていますので、必要に応じてデザインも変更可能です。
まとめ
Laravelを使えば、ログイン機能は簡単に実装できることがお分かりいただけたかと思います。
ほとんどのWebアプリケーションにはログイン機能をつけますので、この部分の手間が省けるのは大きいですね。


