FuelPHP・ユーザー管理5:ログイン後のページの下準備

2014/07/27

FuelPHPでユーザー管理をやりたい5。ログインしたあとのページを用意する前に寄り道する。

基本

前回だとログイン後、「http://○○○/index」を表示しようとする。
「http://○○○/」を表示させたくても「welcome/index」を表示する設定になっている。

ひとまず、トップページと404ページを「home/」にまとめることにする。

ルーティングの修正

fuel/app/config/routes.php

<?php
return array(
  ‘_root_’ => ‘home/index’,
  ‘_404_’ => ‘home/404’,
);

コントローラーを追加

fuel/app/classes/controller/home.php

<?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

<!DOCTYPE HTML>
<html>
<head>
  <meta charset=”utf-8″>
  <title>FuelPhpのトップ</title>
</head>
<body>
  FuelPhpのトップ
</body>
</html>

ビュー(404ページ)を追加

fuel/app/views/home/404.php

<!DOCTYPE HTML>
<html>
<head>
  <meta charset=”utf-8″>
  <title>404ページ</title>
</head>
<body>
  404ページ
</body>
</html>

ログイン成功時のコントローラー修正

「Response::redirect(‘index’)」を修正。

fuel/app/classes/controller/sample1.php

<?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;
  }
}

「FuelPHPでユーザー管理」の一覧

新着(ニュース関連以外)

2018-07-26
年賀状で「新春」とか書くけど・・・何故なんだろうと8月を目前にした今、疑問に思った。
2018-05-16
PHPで画像のヘッダ情報(?)の「Orientation」を元に画像回転させたい。
2018-03-05
Android Studioをインストール。エミュレータを軽くするトコまで終わらせたかったけど、挫折した。
2018-02-23
プッシュ通知について調べてた時にでてきたServiceWorker。そのServiceWorkerについてのメモ。
2017-12-13
jqueryで取得したDOM要素をオブジェクトじゃなくて、配列で受け取りたい