Laravelでログインユーザーのみページを表示させる

# /resources/viewsの下にmenu.blade.phpをつくる。適当にhelloとしておく

# 続いて、/app/Http/Controllersに MenuController.phpをつくる

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MenuController 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('menu');
    }
}

# /routes/web.phpにroutingを追加

Route::get('/home', 'MenuController@index')->name('menu');

# ログインしている時

# ログアウトしている時は自動的にloginにリダイレクトされる

おおおおおおおおおおおおおお、なるほど、ようは、controllerで$this->middleware(‘auth’);があればいいのね。なるほど。ログインサービス作りたくなってきた。

laravelの.envでsmtpを設定

# 初期値

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

# mailtrap.ioから、usernameとpasswordを取得して.envを編集
# password reset buttonを押下

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required

う。。。

# キャッシュクリア
[vagrant@localhost angel]$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

# 再度password reset buttonを押下

# mailtrapで確認

なるほど。

laravelのログイン認証を勉強しよう

# ディレクトリを作成します
[vagrant@localhost app]$ mkdir angel

# 作成したディレクトリに移動
[vagrant@localhost app]$ cd angel
[vagrant@localhost angel]$ ls

# composerをインストール
[vagrant@localhost angel]$ curl -sS https://getcomposer.org/installer | php
[vagrant@localhost angel]$ ls
composer.phar

# composerでlaravelをインストール。ディレクトリ名は適当にangelとしておきます。
php composer.phar create-project –prefer-dist laravel/laravel angel

# mysqlにログイン
[vagrant@localhost ~]$ mysql -u root -p
Enter password:
mysql> show databases;

# mysqlでDBの作成
mysql> create database angel;
Query OK, 1 row affected (0.12 sec)

# .envファイルを編集

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=angel
DB_USERNAME=root
DB_PASSWORD=secret

# migration 実行
[vagrant@localhost angel]$ cd angel
[vagrant@localhost angel]$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table

# 作成されたtable確認
mysql> use angel;
mysql> show tables;
+—————–+
| Tables_in_angel |
+—————–+
| migrations |
| password_resets |
| users |
+—————–+
3 rows in set (0.00 sec)

mysql> describe migrations;
+———–+——————+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+——————+——+—–+———+—————-+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| migration | varchar(255) | NO | | NULL | |
| batch | int(11) | NO | | NULL | |
+———–+——————+——+—–+———+—————-+
3 rows in set (0.08 sec)

mysql> describe password_resets;
+————+————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+————+————–+——+—–+———+——-+
| email | varchar(255) | NO | MUL | NULL | |
| token | varchar(255) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
+————+————–+——+—–+———+——-+
3 rows in set (0.00 sec)

mysql> describe users;
+——————-+———————+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+——————-+———————+——+—–+———+—————-+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| email_verified_at | timestamp | YES | | NULL | |
| password | varchar(255) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+——————-+———————+——+—–+———+—————-+
8 rows in set (0.00 sec)

# 認証機能を生成
[vagrant@localhost angel]$ php artisan make:auth
Authentication scaffolding generated successfully.

# routes/web.phpに追加されている

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

# php artisan serveでサーバを立てる
[vagrant@localhost angel]$ php artisan serve –host 192.168.35.10 –port 8000
Laravel development server started:

# ${domain}/register にアクセス

おおおおおおおおおお、これはちょっと勉強が必要だ。

WebController

Webcontroller is an HTML5-based browser application which allows to build versatile user interfaces for visualization, control and debugging of ROS systems. It uses and shows ROS messasges, services and parameters, much like the rqt-tools. GUIs are created right in the browser by drag&drop and can be saved. A menu bar displays the available ROS resources and widgets.

error handling

General method for error handling

if(env.IsDevelopment()){
	app.UseDeveloperExceptionPage();
} else {
	app.UseExceptionHandler("/Error");
	app.UseHsts();
}

SessionManager type

Sledge::SessionManager type
-> module to decide the method of passing session ID

Sledge::SessionManager::Cookie
-> Pass session ID using cookie.

Sledge::SessionManager::StickyQuery
->Receive session ID from QUERY_STRING. Session ID is passed embedded in HTML.

Sledge::SessionManager::Rewrite
->We do something similar to Sledge::SessionManager::StickyQuery using mod_rewrite.

– Special session management module
Sledge::SessionManager::CookieStore
Embed session data in a cookie. Install it separately.

”use CGI;” or “use CGI qw(:standard);”, what does it mean?

Modules are made as classes. “use” is a class definition declaration.
“CGI” is a very easy name, but it is actually a class(function set) that processes basic CGI parameters in the basics when creating CGIs.
Therefore, “use CGI” defines a class of CGI module.

“use CGI” is an object-oriented style, and “use CGI qw(:standard);” is a function style declaration.

#!/usr/bin/perl --
use strict;
use warnings;
use CGI qw(:standard);