LoginSignup
2
3

More than 5 years have passed since last update.

第6話 友だち

Last updated at Posted at 2016-02-12

友だちは大切に!

太郎と次郎は街で出会った気の合う人と友だちになって、仲間を増やそうと考えていました。忘れっぽい二人は、出会った仲間を友だちとしてメモっていこうと思いついたことをコード化しました。

友だち情報をメモるためにクラスを実装します。

app/models/Friends.php
<?php

class Friends extends Phalcon\Mvc\Collection
{
    public function getSource()
    {
        return "friends";
    }
}

友だち情報をメモるためのフォームを作成します。今回はメールアドレスをメモすることにします。

app/forms/FriendForm.php
<?php

class FriendForm extends Phalcon\Forms\Form
{
    public function initialize($entity = null, $options = null)
    {
            $email = new Phalcon\Forms\Element\Email('email');
            $email->setLabel('お友だちのメールアドレス');
            // 必須チェック
            $email->addValidators([
                new Phalcon\Validation\Validator\PresenceOf([
                    'message' => 'メールアドレスは、必ず入れてください'
                ])
            ]);
            $this->add($email);
            $friend = new Phalcon\Forms\Element\Submit('friend');
            // ボタンのValue変更...これであってる?
            $friend->setDefault('お友だちになる');
            $this->add($friend);
    }
    public function messages($title)
    {
        if ($this->hasMessagesFor($title)) {
            foreach ($this->getMessagesFor($title) as $message) {
                $this->flash->error($message);
            }
        }
    }

}

作成したフォームを自動的に読み込むようにオートローダーに追加します。

public/Index.php
    // オートローダにディレクトリを登録する
    $loader = new Phalcon\Loader();
    $loader->registerDirs([
        '../app/controllers/',
        '../app/models/',
        '../app/forms/'
    ])->register();

表示はVoltエンジンを使ってみたいので追加します。

public/Index.php
// ビューのコンポーネントの組み立て
    $di->set('view', function () {
        $view = new Phalcon\Mvc\View();
        $view->setViewsDir('../app/views/');
        $view->registerEngines([
            '.volt' => 'Phalcon\Mvc\View\Engine\Volt',
            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
        ]);
        return $view;
    });

HTMLのテンプレート(というか、外枠?w)を作成します。

views/index.volt
<!DOCTYPE html>
<html lang="ja">
 <head>
  <title>ヨンプル</title>
 </head>
 <body>
  {{ content() }}
 </body>
</html>

フォーム用のHTMLを作成します。(最後のcontent()はflashしたメッセージ用です)

views/index/index.volt
{{ form() }}
 {{ form.label('email') }}
 {{ form.render('email') }}
 {{ form.render('friend') }}
 {{ form.messages('email') }}
 {{ content() }}
{{ end_form() }}

コントローラーを作成します。

app/controllers/IndexController.php
class IndexController extends Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        $form = new FriendForm();
        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost())) {
                $query['conditions'] = [
                    'email' => $this->request->getPost('email')
                ];
                if (Friends::count($query) != 0) {
                    $this->flash->error('すでにお友だちですけど...');
                } else {
                    $friend = new Friends();
                    $friend->email = $this->request->getPost('email');
                    if ($friend->save()) {
                        $this->flash->success('お友だちになりました。');
                    }
                }
            }
        }
        $this->view->form = $form;
    }
}

なんとなく動いたが、もう少し、Voltエンジン勉強しよう...

2
3
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
2
3