【问题标题】:Start execution from indexAction() in IndexController in ZEND Framework从 ZEND 框架中 IndexController 中的 indexAction() 开始执行
【发布时间】:2011-12-01 20:34:50
【问题描述】:

在 IndexController 的 indexAction() 方法中,我调用了模型的方法,该方法从数据库返回员工列表(employeeList)。然后将此员工列表添加到 $view 中,然后调用 $view->render('index. phtml') 和 index .phtml 显示了employeeList。代码如下:

IndexController.php

<?php

require_once('../Zend/Controller/Action.php');
require_once('../models/HRModel.php');
require_once('../Zend/View.php');

class IndexController extends Zend_Controller_Action
{
 protected $hrModel;

     public function init()
     {
            $this->hrModel = new Application_Model_HRModel();

     }

     public function indexAction()
     {
           $view = new Zend_View(array('scriptPath' =>'../views'));
           $view->employeeList = $this->hrModel->queryAllEmployees();
           echo $view->render('index.phtml');

     }


 }

Application_model_HRModel.php

<?php
require_once('Zend/Db.php');
require_once('Zend/Config/Ini.php');
class Application_Model_HRModel
{
   protected $db=null;

   public function queryAllEmployees() {

   return $this->db->fetchAssoc("select comment from guestbook");

}

}

index.phtml

foreach ($this->employeeList as $emp):
extract($emp);

echo '$EMPLOYEE_ID';

echo $comment;

endforeach

现在我想从 indexAction() 方法开始执行。但是怎么做呢?在浏览器中输入的 url 应该是什么?在请求参数中,控制器将是 IndexController,动作将是 indexAction。所以请帮助我来解决这个问题。

【问题讨论】:

  • 你在哪里设置文档根ZF?
  • 在我的项目中,在应用程序文件夹中有 4 个文件夹,分别命名为 configs、controllers、models、views。在 library 文件夹中,我包含 Zend 文件夹,其中包含 Zend 框架的所有类。因为我必须从索引控制器所以我将 web 根设置为应用程序/控制器,并在运行配置中将项目 url 设置为localhost:8888/Index/index,其中控制器是索引,操作是索引。当我在浏览器中运行它时,它显示为“警告:require_once(../Zend/ Controller/Action.php) [function.require-once]: 无法打开流:
  • 您必须将文档根目录设置为公共目录并转到页面:localhost:8888,但我建议您阅读此内容:framework.zend.com/manual/en/learning.html
  • 感谢您的回复。我现在已设置为 application/public。但仍然出现同样的问题。我也已在 apache httpd 中设置了文档根目录并且我已阅读您提供的手册并应用了所有内容,但......

标签: zend-framework


【解决方案1】:

要执行这个动作,你需要用索引动作调用索引控制器。所以你使用 www.foo.bar/index/index 或简单的 www.foo.bar。 如果这不起作用,您的配置可能有错误。

还是我弄错了你的问题?

您是如何配置您的 PHP 错误处理的?也许你有一个没有显示的 PHP 错误。

【讨论】:

  • 当我在浏览器中请求 url localhost:8888/Index/index 时,它显示警告:require_once(../Zend/Controller/Action.php) [function.require-once]: failed to open流:在第 2 行的 C:\Users\398853\Documents\NetBeansProjects\PhpProject3\application\controllers\IndexController.php 中没有这样的文件或目录我已经在库文件夹中包含了所有 Zend 类(因为我正在使用 netbeans)。
  • 因为我必须从索引控制器开始所以我将 web 根设置为应用程序/控制器并在运行配置中将项目 url 设置为 localhost:8888/Index/index 其中控制器是索引,操作是索引。什么否则必须完成吗?主要是我必须从索引控制器开始执行,然后从该控制器开始应用 $view->render('index.phtml')。所以事情是从 indexcontroller 的 indexAction() 开始执行方法。感谢您的回复。
  • 为什么不使用 Zend 的自动加载功能来包含所有需要的文件?
猜你喜欢
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
相关资源
最近更新 更多