在同一视图中使用多个模块

时间:2012-11-28 16:19:27

标签: php zend-framework

我正在使用Zend Framework 1.12开发一个Web应用程序,这对我来说是一个新的东西,而且我不确定如何做我想做的事情。

编辑:当我谈论模块时,我的意思是控制器,对不起,我仍然误解了这些条款......

在我的主页上,模块索引,我做了我想做的事情,创建了几个动作和所有的东西,但我想添加一个我自己做的搜索引擎。

问题在于,我想将搜索引擎创建为一个名为Search的单独模块,例如,将SearchForm放在主页中。点击提交会将表格中的数据发送到搜索模块。

我不太明白如何做到这一点,而无需去/搜索访问我的表单和每个相关的操作。

我是否必须使用View Helper?

此外,首页中的searchForm将是某种QuicKSearch,访问/搜索将显示更详细的研究形式。

有人可以解释我如何从索引模块访问searchForm或将我重定向到文档的部分谈论它吗?我的研究没有成功,谷歌也没有帮助我。

编辑:当我谈论模块时,我的意思是控制器,对不起,我仍然误解了这些条款......

2 个答案:

答案 0 :(得分:1)

首先,将searchform构建为viewHelper,然后您可以在多个视图中重用它。 表单片段中的action属性设置为searchModule / controller / action。

另外在Zend文档中研究viewHelpers和Forms。

答案 1 :(得分:0)

我实际上更喜欢将其作为action helper,然后使用标准placeholder view helper来展示搜索表单。

让我演示:

实际操作助手只是启动一个表单并准备显示。我会把表单结构留给你。

//the action helper
//Just fill in the args for the form to be displayed
class NameSpace_Controller_Action_Helper_Search extends Zend_Controller_Action_Helper_Abstract
{
    public function direct($action, $label = null, $placeHolder = null)
    {
        $form = new Application_Form_Search();
        //set the action
        $form->setAction($action);
        //set the submit button text
        $form->search->setLabel($label);
        //set the hint text displayed in the form window
        $form->query->setAttribs(array('placeholder' => $placeHolder,
            'size' => 27,
        ));

        return $form;
    }
}

我将帮助器放在控制器的predispatch方法中,这样控制器中的每个操作都可以使用搜索表单,而不必在每个页面中构建它。

//to use the helper in your controller
class IndexController extends Zend_Controller_Action
{

    public function preDispatch()
    {
        //setup action helper and assign it to a placeholder
        $this->_helper->layout()->search = $this->_helper->search(
                '/index/display', 'Search Collection!', 'Title');
    }

//in your view script
<?php echo $this->layout()->search ?>

我喜欢将占位符放在我的主layout.phtml中,以便每当我填充它将显示的占位符时。现在你所要做的就是随心所欲地设计风格。

请记住:与任何html表单一样,action参数只是一个url,因此可以将任何有效的url分配给表单操作。在此示例中,我使用了/controller/action参数,但还有许多其他方法可以将url传递给表单。想到url helper是一种很好的方法。

  

url($ urlOptions,$ name,$ reset,$ encode) :创建基于网址的字符串   在命名路线上。 $ urlOptions应该是一个关联数组   特定路线使用的键/值对。