Passing database results into header file in PHP-MVC -



Passing database results into header file in PHP-MVC -

i'm starting new project using http://www.php-mvc.net framework have never had include database results in header file before , not sure how go it. need pull list of current categories , there id's database , utilize them populate menu.

the header.php file in /views/_templates. normal way of passing database results view run query in relevant model, info in controller, pass info controller view, loop on info foreach loop in view. problem beingness _template files don't have kind of controller them.

the best can come utilize include , include view file home controller, using controller results , pass them menu.php in views/home folder.

/views/_templates/header.php

<li class="dropdown"> <?php include 'views/home/menu.php'; ?> </li>

/views/home/menu.php

foreach ($links $link){ <a href="<?php echo url . $link->url; ?>"><li><?php echo $link->name; ?></li></a> } the above code has been shortened more principle working example.

the method have come works wanted know if there's more elegant way of doing things?

as commented:

no need store in session utilize twig global available in templates , create sure base of operations controller runs preexceute pull info , add together global.

that might this:

abstract class mybasecontroller extends controller { private $view = null; private prepareview() { $twig_loader = new twig_loader_filesystem(path_views); homecoming new twig_environment($twig_loader); } public function getview() { if ($this->view === null) { $this->view = $this->prepareview(); } homecoming $this->view; } protected function prerender() { // whatever logic need prepare menu info $links $this->getview()->addglobal('links', $links); } public function render($view, $data_array = array()) { $this->prerender(); // render view while passing to-be-rendered info echo $this->getview()->render($view . path_view_file_type, $data_array); } }

now depending on info need build stuff $links may or may not need bit more elaborate. given how url params handled in application class. hope you're doing learning experience because "framework" you've found isnt much other learning how might implement mvc... :-/

php

Comments

Popular posts from this blog

javascript - I need to update the text of a paragraph by inline edit -

javascript - THREE.js reposition vertices for RingGeometry -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -