FuelPHP・ユーザー管理3:ユーザーを登録可能にする
FuelPHPでユーザー管理をやりたい3。ユーザー登録するフォームを用意する。
流れ
コントローラ、ビューファイルを設定して、実際に登録できるかをチェックする。
コントローラに記述
▼「fuel/app/classes/controller/sample1.php」
use \Auth;
class Controller_Sample1 extends Controller {
public function action_add_user() {
// viewを準備
$view = View::forge(‘sample1/add_user’);
if ($_POST) {
//POSTデータを受け取る
$username = Input::post(‘username’);
$password = Input::post(‘password’);
$email = Input::post(‘email’);
// Authのインスタンス化
$auth = Auth::instance();
//ユーザー登録
$auth->create_user($username, $password, $email);
}
// 登録フォームの表示
return $view;
}
}
ビューの作成:ユーザー登録画面
▼「fuel/app/views/sample1/add_user.php」
<html>
<head>
<meta charset=”utf-8″>
<title>ユーザー登録</title>
</head>
<body>
<form name=”form1″ method=”post” action=””>
<table width=”100%” border=”1″>
<tr>
<th colspan=”2″ scope=”row”>ユーザー登録</th>
</tr>
<tr>
<th scope=”row”>ユーザー名</th>
<td><label for=”username”></label>
<input name=”username” type=”text” id=”username”></td>
</tr>
<tr>
<th scope=”row”>Eメール</th>
<td><label for=”email”></label>
<input type=”text” name=”email” id=”email”></td>
</tr>
<tr>
<th scope=”row”>パスワード</th>
<td><label for=”password”></label>
<input type=”password” name=”password” id=”password”></td>
</tr>
<tr>
<th colspan=”2″ scope=”row”><input type=”submit” name=”button” id=”button” value=”登録”></th>
</tr>
</table>
</form>
</body>
</html>
試しに登録する
「http://○○○/sample1/add_user」でアクセスして登録する。
登録できたか確認
登録したらDBのテーブルから登録されたかチェックする。