FuelPHP・ユーザー管理5:ログイン後のページの下準備
FuelPHPでユーザー管理をやりたい5。ログインしたあとのページを用意する前に寄り道する。
基本
前回だとログイン後、「http://○○○/index」を表示しようとする。
「http://○○○/」を表示させたくても「welcome/index」を表示する設定になっている。
ひとまず、トップページと404ページを「home/」にまとめることにする。
ルーティングの修正
fuel/app/config/routes.php
return array(
‘_root_’ => ‘home/index’,
‘_404_’ => ‘home/404’,
);
コントローラーを追加
fuel/app/classes/controller/home.php
class Controller_home extends Controller {
public function action_index() {
return Response::forge(View::forge(‘home/index’));
}
public function action_404() {
return Response::forge(View::forge(‘home/404’), 404);
}
}
ビュー(indexページ)を追加
fuel/app/views/home/index.php
<html>
<head>
<meta charset=”utf-8″>
<title>FuelPhpのトップ</title>
</head>
<body>
FuelPhpのトップ
</body>
</html>
ビュー(404ページ)を追加
fuel/app/views/home/404.php
<html>
<head>
<meta charset=”utf-8″>
<title>404ページ</title>
</head>
<body>
404ページ
</body>
</html>
ログイン成功時のコントローラー修正
「Response::redirect(‘index’)」を修正。
fuel/app/classes/controller/sample1.php
use \Auth;
class Controller_Sample1 extends Controller {
public function action_add_user() {
$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;
}
public function action_login() {
$view = View::forge(‘sample1/login’);
$data = array();
if ($_POST) {
// Authのインスタンス化
$auth = Auth::instance();
// 資格情報の確認
if ($auth->login($_POST[‘username’], $_POST[‘password’])) {
// 認証OKならトップページ:
//Response::redirect(‘index’);
Response::redirect(”); // 「http://○○○/」に飛ばす
}
else {
//認証が失敗したときの処理
$view->set(“username” , $_POST[‘username’]);
$view->set(“login_error”, ‘ユーザー名かパスワードが違います。再入力して下さい。’);
}
}
else {
$view->set(“username” , “”);
$view->set(“login_error”, “”);
}
return $view;
}
}