4

I've read several articles about HMVC, in PHP and other languages, and in all of them the implementation of this pattern was about having a "Master-Controller" requesting the Response Body of one or more resources, internal or external, using cURL or Stream Contexts, in order to build its own, directly through a Response object or through a custom View Engine, assigning template variables with the HTML obtained.

However, all of these articles, clung so much to the technical concept, that most of times they did not mention all pertinent points. So the conclusion I came with was the basic concept of HMVC is, generally speaking, an MVC inside MVCs, working seamlessly both hierarchical or isolatedly, always in the same way.

But how exactly would happen the HTML componentizing of this "Master-Controller" if, in order of each subsystem to work in the same way by all means it would need eventual scripts, stylesheets or even extra markup?

With an example it gets easier to understand:

Considering an Application developed with Bootstrap 3 made of N components, all of them also developed with the same framework (so they work isolatedly the exact same way it would when part of main Application), with an HMVC figuratively created with the pseudo-code below:

// Make the Requests

$projects = new Request( 'management/projects', 'GET' );
$clients  = new Request( 'management/clients',  'GET' );

// Creates the Template Variables with Response Bodies HTML

$this -> view -> assign(

    array(

        'projects' => $projects -> send(),
        'clients'  =>  $clients -> send()
    )
);

As a matter of clarification in this fragment Request::send() would return the HTML

Would result in something similar to:

<html lang="en">

    <head>

    <meta charset="utf-8">

    <link rel="stylesheet"
        href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

    </head>

    <body>

        <div class="container-fluid">

            <!-- Sidebar -->

            <div class="row">

                <div class="col-sm-3 col-md-2 sidebar" id="sidebar" role="navigation">

                    <ul>
                        <li>Sidebar Item</li>
                        <li>Sidebar Item</li>
                        <li>Sidebar Item</li>
                    </ul>

                </div>

            </div>

            <!-- Main Content -->

            <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

                <html lang="en">

                    <head>

                        <meta charset="utf-8">

                        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

                    </head>

                    <body>

                        <div class="container-fluid">Component HTML</div>

                    </body>

                </html>

            </div>

        </div>

    </body>

</html>

Which, in this example, is "just" wrong, after all, in a real Application we could have script conflicts or CSS rules messing any HTML coming after the section designed to the component (like a footer or another subsequent component) and so on...

That said, how the HTML componentizing should happen in a HMVC?

The smallest idea that I come across would be this "Master-Controller" to parse the HTML received of each component and get only what it really need from it, like everything inside a specific wrapping DIV inside the <body></body>.

But I haven't seen anything even remotely close to this in anything I've searched for.

1 Answers1

0

The idea behind HMVC is that it provides a solution for creating widgets using MVC in JAVA. This is done by layering multiple MVC widgets on top of one another (using hierarchy). However as with all design patterns, you should only use them to solve a problem or create the much needed structure. The main gist of your question implies that you want to use the pattern, for just using the pattern. You should ask yourself 'Why do I want to use this pattern and what does it solve for me?' If the answer is 'I don't have a problem and just want to use it because it seems awesome' then you're doing it.

The practicality of HMVC with PHP is limited. You can still use it to some extend of the original idea. Keep in mind that PHP/Web is a stateless environment, while Java is not not.

Main Controller (MVC Layer 0) - Sub Controllers (MVC Layer 1) - Sub Controllers (MVC Layer N)

The View is dictated by the Controller and so is the Model. So within each controller you need to create a way that it can handle the incoming request, if needed pass it through to another MVC layer, and load the correct model/view. Your concern regarding the CSS/Script errors that might occur. This pattern is based on hierarchy, so as a coder you need to be aware of this and make sure that each lower MVC layer in the hierarchy can work with the layer above.

Paul
  • 735
  • 4
  • 11
  • 1
    I'm quite sure you didn't answer my question. I don't have YAGNI problems about the pattern, by using it I would be able to extend the functionality of the main Application without touch it. Unless someone convince me otherwise, I still stand that each "pluggable" MVC must work the **exact same way** within the main Application as well as isolatedly. All articles I've found show something like the pseudo-code I presented but forget to show what the "plugged" HTMl has. Or when the HTML is shown, it doesn't have the "extra markups", only the contents of the `` to be used right away. –  Jan 04 '15 at 13:35
  • @BrunoAugusto I think he just wanted to earn the bounty with writing something general, but this doesn't work here. – Rizier123 Jan 04 '15 at 13:43
  • @BrunoAugusto "I still stand that each "pluggable" MVC must work the exact same way within the main Application as well as isolated". That is not correct. The HMVC works based on hierarchy, which I clearly mentioned in my answer. It is not aimed at creating standalone widgets without hierarchy. But based on your comment, you seem to be seeking for a solution that HMVC is not. – Paul Jan 04 '15 at 13:53
  • That's why I said "Until someone convince me otherwise". In all my researches about HMVC I saw the pattern described as I said and with the lack of good examples it's understandable to take this false assumption as correct. Do you have or know about an article that explains the subject, more practical and less technically, with HTML examples (that's the biggest point)? –  Jan 04 '15 at 14:49