0

我正在编辑 Zend Framework 2 模块,遇到以下代码:

<?php
echo $this->partial('/menu', array('menu' => $this->menu));
?>
<hr />
<div class="myclass">
    <h2>Heading</h2>
<?php

$form = $this->form;
$form->setAttribute('action', $this->url('register'));
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formRow($form->get('name'));
echo '<br />';
echo $this->form()->closeTag();

$this->inlineScript()->offsetSetScript(99, "
function checkFieldMatch() {
    var field = $('#txtNewField').val();
    //...
}
");

?>
</div>

其中大部分是现有代码,现在我添加了一些 jQuery。代码有 HTML、CSS、JavaScript/jQuery 和 PHP。代码位于我的/vendor/$module/$submodule/view/$submodule/register/index.phtml文件中。

在印象中 Zend Framework 2 是 PHP 世界中最新和最伟大的重量级框架,我不禁想知道在代码分离原则方面到底发生了什么。当然,上述代码的这种可恶之处一定是用户错误。包括我在内的代码作者帮助在一个文件中创建了这种令人难以忍受的混乱技术。我想这表明,无论一个复杂的框架多么善意,当涉及到松散耦合组件时,仍然有人可以将 5 种技术放入一个简短的文件中,让它看起来像地狱一样。

虽然可能有几种方法可以做到这一点,但我的问题是:

在 Zend Framework 2 中分离 HTML、CSS、jQuery、JavaScript、PHP 代码的正确(推荐、设计)方法是什么,以便在可能的情况下将每种技术分离到自己单独的文件和文件夹中,同时保持适当的定义良好的工作连接(松散耦合),并且仅在绝对需要或通过行业最佳实践推荐的情况下将技术直接嵌入彼此?

4

1 回答 1

1

There's absolutely nothing wrong with using partials in your View-Layer. A View is not just text. It has scripting elements, too. While yes, you can use a (nother) templating language (like smarty) atop of Zend Framework, it is somewhat unnecessary since PHP in itself is a templating language (P... Hypertext Processor).

The above example uses FORMS as a bad example. Now i challenge you to show me ANY Framework / Language that actually handles Form in a great way. FORMS, no matter the language, no matter the framework are ALWAYS a mess because they ALWAYS include several layers of the MVC. A Form is NOT just View, so you can't completely separate this.

$this->formXY() is actually just a ViewHelper, so from the technical point of view this IS the ViewLayer.

The only real thing i agree with is the JavaScript handling. But then again you can choose to include a JavaScript file on your own. Or you can just have one big javascript file for all your pages. This - again - is not a problem that only ZF has ;)

于 2013-11-08T07:45:03.597 回答