LoginSignup
0
1

More than 3 years have passed since last update.

fhalconでMVCに沿ったアプリケーションの作成方法

Posted at

fhalconでMVCに沿ったアプリケーションの作成方法です。

チュートリアルやりましたが、英語しかなく、簡単なことをいちいち英語で参照するのはめんどくさいので、備忘録。

投稿ができるようになっていて、
「一覧、新規作成、削除,検索」
ができます。
編集も以下のコードを参考にやれば簡単です。

コントローラー作成

app/controllers/PostController.php
<?php

use Phalcon\Mvc\Controller;

class PostController extends Controller
{
    public function indexAction()
    {
      #全てを取得
      $this->view->posts = Posts::find();
    }

   #新規投稿ページ表示
    public function newAction()
    {
    }
    #新規投稿保存
    public function createAction()
    {
        $post = new Posts();

        // Store and check for errors
        $success = $post->save(
            $this->request->getPost(),
            [
                "message"
            ]
        );

        if ($success) {
            echo "Thanks for registering!";
        } else {
            echo "Sorry, the following problems were generated: ";

            $messages = $post->getMessages();

            foreach ($messages as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }

        $this->view->disable();
    }
}
    #formから送られてきた新しいデータを保存
    public function createAction()
    {
        $post = new Posts();


        $success = $post->save(
            $this->request->getPost(),
            [
                "message"
            ]
        );

        if ($success) {
            echo "Thanks for registering!";
        } else {
            echo "Sorry, the following problems were generated: ";

            $messages = $post->getMessages();

            foreach ($messages as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }
        $this->view->disable();
    }

    #hogeAction($パラメータ)でurlにあるパラメータの値を取得
    public function deleteAction($id)
    {
      $post = Posts::findFirstById($id);
      $post->delete();
      $this->response->redirect("/post");
    }

    public function sendAction()
    {
    #フォームから送られてきた値は$this->request->getPost('name属性');で取得可能
      $id = $this->request->getPost('id');
      $post = Posts::findFirst($id);
      // privateメソッドを作り、参照することも可能
      $this->sendToLine($post);

      echo "Thanks for sending!";
      $this->view->disable();
      // return $this->response->redirect("/");
    }

    //privateメソッド
    private function sendToLine($post)
    {
        #何らかの処理
    }

        #検索
    public function searchAction()
    {
        $numberPage = 1;
       #POSTメソッドで送られてきたかどうかチェック
        if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, "Posts", $this->request->getPost());
            $this->persistent->searchParams = $query->getParams();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }
        $parameters = array();
        if ($this->persistent->searchParams) {
            $parameters = $this->persistent->searchParams;
        }
        #postリクエストで送られてきた$parametersを元にモデルから検索
        $posts = Posts::find($parameters);
        if (count($posts) == 0) {
            $this->flash->notice("The search did not find any posts");
            return $this->dispatcher->forward(
                [
                    "controller" => "products",
                    "action"     => "index",
                ]
            );
        }
        //
        $paginator = new Paginator(array(
            "data"  => $posts,
            "limit" => 10,
            "page"  => $numberPage
        ));

        #何だこれ。わかってない
        $this->view->page = $paginator->getPaginate();
        #viewで表示するように$this->view->postsに代入(viewでは$postsで参照可能)
        $this->view->posts = $posts;
    }




モデル作成

app/model/Post.php
<?php

use Phalcon\Mvc\Model;

class Posts extends Model
{
   #カラム名追加
    public $id;
    public $message;
}

view作成

app/views/post/index.phtml

<!-- 検索フォーム -->
<form action='/post/search' method='post'>
    <h2>message検索</h2>
    <!-- <fieldset> -->
      <!-- id検索 -->
        <div class='control-group'>
            <label for='id' class='control-label'>Id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type='text' id='id' name='id' />
        </div>
        <!-- message検索 -->
        <div class='control-group'>
            <label for='message' class='control-label'>message</label>
            <input type='text' id='message' name='message' />
        </div>

        <div class='control-group'>
            <input type='submit' value='Search' class='btn btn-primary' />
        </div>

    <!-- </fieldset> -->
</form>

<!-- 検索フォーム -->
<form action='/post/search' method='post'>
    <h2>message検索</h2>
    <!-- <fieldset> -->
      <!-- id検索 -->
        <div class='control-group'>
            <label for='id' class='control-label'>Id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type='text' id='id' name='id' />
        </div>
        <!-- message検索 -->
        <div class='control-group'>
            <label for='message' class='control-label'>message</label>
            <input type='text' id='message' name='message' />
        </div>

        <div class='control-group'>
            <input type='submit' value='Search' class='btn btn-primary' />
        </div>

    <!-- </fieldset> -->
</form>
<?php
echo "<h1>post index</h1>";

if ($posts->count() >= 0) {
    ?>
    <table class="table table-bordered table-hover">
        <thead class="thead-light">
        <tr>
            <th>#</th>
            <th>message</th>
        </tr>
        </thead>
        <tfoot>
        <tr>
            <td colspan="3">Posts quantity: <?php echo $posts->count(); ?></td>
        </tr>
        </tfoot>
        <tbody>
        <!--index controllerで定義した$this->view->postsは$postsで参照可能 -->
        <?php foreach ($posts as $post) { ?>
            <tr>
               <!--$post->hogeでhogeカラムのデータ取得可能 -->
                <td><?php echo $post->id; ?></td>
                <td><?php echo $post->message; ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php
}

app/views/post/new.phtml
 <!--ヘルパーを使ったフォームの書き方。-->
<h1>post/new</h1>

<?php echo $this->tag->form("post/create"); ?>

    <p>
        <label for="message">message</label>
        <?php echo $this->tag->textField("message"); ?>
    </p>

    <p>
        <?php echo $this->tag->submitButton("post your message"); ?>
    </p>

</form>

0
1
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
0
1