0

私は Zend フレームワークの初心者です。Netbeans でサンプル プロジェクトを作成しました。index.phtml を表示して適切に動作しています。しかし、コントローラーを呼び出す必要があります。私が試したことは以下のとおりです。

IndexController.php


<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {

    }

    public function indexAction()
    {
        firstExample::indexAction();
    }    

}

そして、index.phtml(空のファイルのみ)のすべてのコンテンツを削除しました。このビューをレンダリングしたくないからです。私のカスタムコントローラーは次のとおりです。

firstExampleController.php

<?php
class firstExample extends Zend_Controller_Action{
    public function indexAction(){
        self::sum();
    }

    public function sum(){
        $this->view->x=2;
        $this->view->y=4;
        $this->view->sum=x + y;
    }
}
?>

firstExample.phtml

<?php
echo 'hi';
echo $this->view->sum();
?>

firstExample.php で sum メソッドを表示する方法。

以下のURLにアクセスすると、空白のページが表示されます。

http://localhost/zendWithNetbeans/public/

上記の URL にアクセスした後、実行は最初に public フォルダー内の index.php に移動すると思います。また、index.php の内容は変更していません。

4

1 に答える 1

1

コントローラー (MVC) を正しく使用していません。コントローラーは、sum メソッドの場合、ビジネス ロジックを実行するべきではありません。コントローラーのみが責任を負い、リクエストを制御し、モデルとビューを結び付けます。そのため、現在それを呼び出すのに問題があります。

モデルを作成して sum メソッドを追加し、必要なコントローラーで使用します。コントローラーからモデルをビューに渡すことができます。

例を次に示します: http://framework.zend.com/manual/en/learning.quickstart.create-model.htmlデータベースを使用しますが、データベースで使用する必要はありません。

基本的に、合計の例は次のようになります。

class Application_Sum_Model {

 public function sum($x, $y) {
   return ($x + $y);
 }
}

class IndexContoler extends Zend_Controller_Action {

   public function someAction() {

    $model = new Application_Sum_Model(); 

    //passing data to view that's okay
    $this->view->y   = $y;
    $this->view->x   = $x;
    $this->view->sum = $model->sum($x, $y); //business logic on mode

   }
}

コントローラーがどのように機能するかをお読みください。http://framework.zend.com/manual/en/zend.controller.quickstart.html

于 2012-09-01T12:57:43.580 に答える