1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[自分メモ]FuelPHPでログイン・ログアウト

1
Posted at

ここはそんなに難しいことはなさそう。

やること

  • ログイン
  • ログアウト

ログイン

Auth::login()を使えば割と簡単にできた。

  • Auth::get_user_id()はarrayが返ってくるのは注意が必要。int/falseで返してほしいなあ。

View

ユーザー名とパスワードをpostして判定する。emailは必要なさそう。

<?php if (Session::get_flash('message')): ?>
<div class="row alert alert-danger">
    <p><?php echo Session::get_flash('message') ?></p>
</div>
<?php endif; ?>

<div class="row" style="padding: 2rem;">
<?php echo Form::open(['action' => 'user/login', 'method' => 'post']); ?>
<div class="form-group">
    <label for="username">ユーザー名</label>
    <?php echo Form::input('username', null, ['id' => 'username', 'class' => 'form-controll']); ?>
</div>
<div class="form-group">
    <label for="password">パスワード</label>
    <?php echo Form::password('password', null, ['id' => 'password', 'class' => 'form-controll']); ?>
</div>
<?php echo form::button('submit', null, ['class' => 'btn btn-primary']); ?>
<?php echo Form::close(); ?>
</div>

Controller

テンプレート設定の処理は冗長だけど今回は大目に見て。。。

    public function post_login()
    {
        if (empty($_POST['username']) || empty($_POST['password'])) {
            Session::set_flash('message', '入力は全て必須です');
            $data["subnav"] = array('register'=> 'active' );
            $this->template->title = 'User &raquo; Register';
            $this->template->content = View::forge('user/login', $data);
            return;
        }

        if (! Auth::login($_POST['username'], $_POST['password'])) {
            Session::set_flash('message', 'ログインに失敗しました');
            $data["subnav"] = array('register'=> 'active' );
            $this->template->title = 'User &raquo; Register';
            $this->template->content = View::forge('user/login', $data);
            return;
        }

        Response::redirect('/');
    }

ログアウト

View

<a href="/user/logout">ログアウト</a>

Controller


    public function action_logout()
    {
        Auth::logout();
        $data["subnav"] = array('logout'=> 'active' );
        $this->template->title = 'User &raquo; Logout';
        $this->template->content = View::forge('user/logout', $data);
    }
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?