0

I am editing a Zend Framework 2 module and I came across the following code:

<?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>

Most of it was existing code, and now I have added some jQuery to it. The code has HTML, CSS, JavaScript/jQuery, and PHP. The code is located in my /vendor/$module/$submodule/view/$submodule/register/index.phtml file.

Being under impression that Zend Framework 2 is latest and greatest heavyweight framework in PHP world, I cannot help but wonder what the heck happened when it came to principles of code separation. Surely, this abomination of a code above must be user-error. The authors of the code, me included, helped create this unbearable mess of technologies in one file. I suppose it shows that no matter how well-intentioned a complex framework is, when it comes to loose coupling components, someone can still put in 5 technologies into a short single file and have it look like hell.

While there may be several ways to do this, my question is:

What is the proper (recommended, designed) way to separate HTML, CSS, jQuery, JavaScript, PHP code in Zend Framework 2 so that each technology is separated into its own separate file and folder, where possible, while maintaining proper well-defined working connections (loose-coupling), and technology is embedded into each other directly only where it is absolutely required, or recommended via industry best-practices?

Dennis
  • 7,907
  • 11
  • 65
  • 115
  • I have to say, from a person who's been writing personally since 1990 and professionally for about 6 years, that is horrible looking layout. I like to keep everything in MVC style layout, with strong emphasis on V(view) being JUST THAT, a view. No code, no calculating, no scripting, just a view that places variables fed to it where they belong thus rendering the end result to the user. And personally, I wouldn't call Zend2 "the latest and greatest". I have tried many others over the years, but I keep coming back to Codeigniter. It's just nice and "free" in environment and layout'. – SpYk3HH Nov 07 '13 at 22:36
  • If you want "loose-coupling" with great layout, you should really try CI. Simply add a few folders to the base directory and your good to go. Folders like, "js", "images", "style" or "css", and etc. Then you can make "helper" controller that extends "base_url()" to make commands like "js_url()" that immediately calls ur js folder and use the parameter to get the file, or ("subdir/file") as it may be. See this blog post -> http://ellislab.com/forums/membe%20r/140380/viewthread/236299/ – SpYk3HH Nov 07 '13 at 22:40
  • Thank you. This is for work, where we have decided to go the ZF2 route. I am new to frameworks in general, and have heard of CI but never really checked it out. I'm afraid it is not the time to do so. But in the future I wanted to check out Laravel 4 and may give CI a look as well. – Dennis Nov 08 '13 at 14:32
  • I am thinking this: partials look a little messy to me. Not sure if it is the best way to use them here, but moving on ... perhaps for jQuery instead of using actual jQuery code here I can create a viewHelper to say $this->getjQuery("myCheckFieldMatch") method where it will pull the actual jQuery string from elsewhere. Doing so will move jQuery out of this file. When it comes to forms, I have not been crazy about using PHP to generate HTML. I've been using HTML to do HTML. Maybe it is one of the necessary evils of using a framework. – Dennis Nov 08 '13 at 15:29
  • As for HTML line-breaks, I can append them to the line above, like `echo $this->form()->stuff() . '
    ';` to get them out of their one line. That way they are less of an eye-sore.
    – Dennis Nov 08 '13 at 15:30

1 Answers1

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

Sam
  • 16,435
  • 6
  • 55
  • 89
  • I completely agree with you and **disagree** with the op's comments on it being an "abomination" - It just so happens that the view is composed of entirely view helpers/view partials - both of which, by design, are there to avoid repetitive and complex logic in the view - If used correctly they promote reusability. – AlexP Nov 08 '13 at 14:33
  • Ugh.. I feel like maybe there is no good answer. Before this, my way of writing FORMS was this: [form in HTML file a la View], which POSTs to [PHP Controller file accepting form data, doing processing & database I/O], which redirects to a [View file/layer]. HTML Form was basically a View with HTML+CSS+JavaScript separated nicely with PHP vars & loops. Controller was PHP code with Database layer interaction, and View had same technologies as HTML Form. – Dennis Nov 08 '13 at 14:51
  • @AlexP: I think what bugs me about the code are things like `echo '
    ';`, the `jQuery` block, and the `little bits of HTML inside an otherwisely-PHP code`. Using canine terms, to me it reads like a mutt. I suppose if I really go ahead and start cleaning it up, once I learn ZF2 constructs more, I'd like the code that has a bit more of a pedigree look to it. That `cleaner code look` is where I want to go.
    – Dennis Nov 08 '13 at 15:10
  • 1
    @Dennis The thing is, everything that is used in there is a ViewHelper ultimately. A ViewHelper in it's sense belongs to the VIEW itself. It does nothing but output HTML, so it's not wrong in there. The javascript problematic indeed is there and personally i haven't found a solution for myself that i really LIKE :P – Sam Nov 08 '13 at 15:44
  • Challenge Accepted. I understand he's stuck in ZF2, and that sux. But forms in CI is super simple, not messy, and doesn't take "several" layers other than "basic" MVC. You simply write your HTML in the "View" which IS HTML. Then use either the method attr or jQuery to connect it to the Controller and you're done. As for injection checks and from verifying, you have your normal jQuery/JS options, as well as CI's nice "input filter" which, when set in config, uses one command to pull Get/Post with "injection" checking already preformed. It's simple, not messy, and most of all, easy. – SpYk3HH Nov 08 '13 at 15:45
  • As for ZF2 and JS injection on the view, when I've used it, I always made a "master view load" helper that allowed me to load any view in one command, but would automagikally include the `` and `footer` above and below the view, thus providing my jQuery call in the head call. The one setback is making sure your head can include all the js files you need for each page, however, i never found this to be a problem. If it is, if you need separation, simply make a param to the `helper` allowing for injection of JS from different dirs into the head call. Make it similar to ASP.MVC – SpYk3HH Nov 08 '13 at 15:48
  • @SpYk3HH The thing is, you can just write plain HTML in ZF2, too. You can just use the POST-Array and pass it through an InputFilter in ZF2, too. Your "CI" has no advantage over ZF2 in that part. The thing that Zend\Form does it that it provides an abstraction and convenience layer on top of that thing. For more than enough use-cases that is a good thing. While this may make views a little more PHP-influenced (which in itself doesn't violate MVC), it arises some other problems... – Sam Nov 08 '13 at 15:50
  • The above suggestion i made, allows you to still use ` – SpYk3HH Nov 08 '13 at 15:50
  • Zend views are intended to uncouple the PHP from the view related code. There are JS libraries, such as `requirejs`, that resolve the speration of the HTML from the JS. This is, in my opinion, something completely different. – AlexP Nov 08 '13 at 18:36