简体   繁体   English

如何从Zend Framework中的索引控制器调用自定义控制器

[英]How to call custom controller from index controller in Zend Framework

I am newbie in Zend framework.And i have made sample project in netbeans.And it is working properly displaying index.phtml .But , I need to call my controller.What I have tried is below. 我是Zend框架的新手,并且已经在netbeans中创建了示例项目,并且可以正常显示index.phtml。但是,我需要调用我的控制器,下面尝试了一下。

IndexController.php


<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {

    }

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

}

And I have deleted all the content of index.phtml(just a blank file) , coz i don't want to render this view. 而且我已经删除了index.phtml(只是一个空白文件)的所有内容,因为我不想呈现此视图。 My custom controller is: 我的自定义控制器是:

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();
?>

How to display sum method in firstExample.php. 如何在firstExample.php中显示sum方法。

It just shows blank page after hitting below URL. 点击下面的URL后仅显示空白页。

http://localhost/zendWithNetbeans/public/

I think after hitting on above URL , execution first goes to index.php in public folder.And I didn't change the content of index.php 我认为在点击上面的URL之后,执行首先转到公用文件夹中的index.php。并且我没有更改index.php的内容

You are using controller (MVC) incorrectly, Controller should not do any business logic, in your case sum method. 您错误地使用了控制器(MVC),在这种情况下,控制器不应执行任何业务逻辑。 Controller only responsible controlling request and glueing model and view together. 控制器只负责控制请求和粘合模型并一起查看。 That's why you have problems now calling it. 这就是为什么现在调用它时遇到问题。

Create Model add method sum, and use in any controller you want. 创建模型添加方法sum,并在所需的任何控制器中使用。 From controller you may pass model to view. 您可以从控制器将模型传递给视图。

Here is example: http://framework.zend.com/manual/en/learning.quickstart.create-model.html it uses database, but it's not necessary to use with database. 例如: http : //framework.zend.com/manual/en/learning.quickstart.create-model.html它使用数据库,但不必与数据库一起使用。

Basically your sum example could look like: 基本上,您的总和示例如下所示:

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

   }
}

Please read how controller is working, http://framework.zend.com/manual/en/zend.controller.quickstart.html 请阅读控制器的工作方式, http://framework.zend.com/manual/en/zend.controller.quickstart.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM