简体   繁体   中英

PHP MVC load multiple controllers models and views in same page

I'm currently designing my own MVC framework in PHP. I've read a lot about MVC frameworks and made one where the controller handles the models and renders the views, although this is not recommended.

The structure works as follows: The bootstrap class loads the proper controller, methods and variables based on the URL. It also initiates a maincontroller, mainmodel and mainview, which have some general functions. The controllers initiate their model and in the end render their template.

The problem is that I have 3 MVC objects. A top menu, a search bar and a table. I want these to be rendered on the same page. But since I let the controller do the rendering I can't render the three different parts in the same webpage. Do I need some kind of helper class, and if so how do I implement it the right way?

The url would be like: localhost/MyApp/grid.

Every controller contains this code:

$this->view->render(filepath/file.php)

Every template contains the code:

echo $this->data

I'm currently designing my own MVC framework in PHP.

Big mistake there, trust me - I tried. It does not end well.

I've read a lot about MVC frameworks (..)

Even bigger mistake. Frameworks do not implement MVC. Therefore, whatever you read was just someone trying to sell their polished shit as gold.

(..) and made one where the controller handles the models and renders the views (..)

Controller is not responsible for rendering templates (because I do not believe that you actually have views) and there are no plural "models".

</rant>

What you need is a proper templating engine. Try twig . Let the templating engine to handle the shared parts.

There are some different concepts to handle this.

In one of my projects i split the html in templates and the view loads each area as it needs it.

For example something like:

$this->renderTemplate('page.header', $pageHeaderModel);
$this->renderTemplate('page.content.'.$viewKey, $pageContentModel);
$this->renderTemplate('page.footer', $pageFooterModel);

On every page the content model changes and the other stay the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM