我是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.中显示求和方法
它只是在点击下面的URL后显示空白页面。
http://localhost/zendWithNetbeans/public/
我认为在点击上面的URL之后,执行首先转到公共文件夹中的index.php。我没有更改index.php 的内容
您错误地使用了控制器(MVC),控制器不应该做任何业务逻辑,在您的案例中是sum方法。控制器只负责控制请求并将模型和视图粘合在一起。这就是为什么你现在称它有问题。
Create Model添加方法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