1

コハナフレームワークの新機能です。

コントローラを作成し、変数を表示したい。

私のコントローラーStart.php:

    <?php defined('SYSPATH') or die('No direct script access.');

class Controller_Start extends Controller_Template {

    public function action_index()
    {
        $view = View::factory('start')
                    ->set('title', array('tytul 1', 'tytul 2'));

                $this->response->body($view);
    }

}

APPPATH / viewsにはStart.phpがあります:

<h1><?php echo $title[0] ?></h1>

サイトを開くとエラーが発生します:

View_Exception [ 0 ]: The requested view template could not be found

アクションからのデータを次のように表示する場合:

$this->response->body('test');

私がサイトを開くとき、「テスト」があります

私の見解の何が問題になっていますか?

4

1 に答える 1

0

コントローラにテンプレート定義Piotrがありません。

コントローラで行う必要があるのは、コントローラが使用する必要のあるビューを定義することです。

class Controller_Start extends Controller_Template {

    // This has to be defined
    public $template = 'start';

    public function action_index()
    {
        $this->template->set('title', array('tytul 1', 'tytul 2'));

        // No need to do this if you're extending Controller_Template
        // $this->response->body($view);
    }
}

my_viewフォルダにあるテンプレートファイルのファイル名(.php拡張子なし)です/application/views。サブフォルダにある場合は、そこに入力します。例:

public $template = 'subfolder/start';

メソッドはそれをViewオブジェクトController_Template::before()に変換するため、文字列として定義する必要があることに注意してください。これは、デフォルトのオブジェクトをオーバーライドする場合を除いて、内に独自のオブジェクトを作成する必要がないことを意味します。次に、次のようなことを行う必要があります。$viewaction_index()

class Controller_Start extends Controller_Template {

    public function action_index()
    {
        $view = View::factory('start')
                    ->set('title', array('tytul 1', 'tytul 2'));

        $this->template = $view;

        // And that's it. Controller_Template::after() will set the response
        // body for you automatically
    }
}

テンプレートを完全に制御したいが、コハナに干渉させたくない場合は、Controller_Templateクラスを拡張せずに、プレーンControllerを使用してビューを自分でレンダリングすることをお勧めします。例:

class Controller_Start extends Controller {

    public function action_index()
    {
        $view = View::factory('start')
                    ->set('title', array('tytul 1', 'tytul 2'));

        // Render the view and set it as the response body
        $this->response->body($view->render());
    }
}
于 2013-02-20T07:46:24.923 に答える