![](/img/trans.png)
[英]Beginners Tutorial for Zend Framework implementing model and call it from Controller
[英]How to call custom controller from index controller in Zend Framework
我是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之后,執行首先轉到公用文件夾中的index.php。並且我沒有更改index.php的內容
您錯誤地使用了控制器(MVC),在這種情況下,控制器不應執行任何業務邏輯。 控制器只負責控制請求和粘合模型並一起查看。 這就是為什么現在調用它時遇到問題。
創建模型添加方法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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.