Welcome Guest, Not a member yet? Register   Sign In
CI3 - wiredesignz HMVC
#1

Hello,

I've just setup a local copy of CI3 & installed wiredesignz HMVC and following the installation instructions I have the welcome message working as it should, I'm just getting started and would like to be able to include a single header.php file & footer.php to use across the application, but unsure where to actually put them within the application.

I currently have the following:
/application/modules/welcome/controllers/Welcome.php
/application/modules/welcome/views/welcome_message.php

/application/modules/templates/views/header.php
/application/modules/templates/views/footer.php

If I put
Code:
<?php require_once 'header.php'; ?>
in welcome_message.php so it loads the header.php where required so basically I don't have to add a header or footer file to each module??
and I can keep them in one place


Regards
Reply
#2

You need to create a controller Templates with the method header() and footer()

With hmvc basically you call the method header and footer in any views:

<body>
echo Modules::run('template/header');
<p>Something...</p>
echo Modules::run('template/footer');
</body>

I have an article about this (in spanish)  http://davicotico.com/configurar-codeigniter-y-hmvc/

Reply
#3

Hi Davicotico,

Thanks for the reply, I'll look at this more in detail, although I don't speak Spanish I can kind of figure it out what you mean.

Regards
Reply
#4

Hello ambush-reality, welcome to the forum...

I think that Davicotico misunderstood your problem. First of all, when working with views you never use required or include to get the view in your project. The normal way is to use $this->load->view('view_name');.

Because you are using HMCV you can load a view almost the same way. HMVC sometime can sometimes find it without help when you use only the name of the view, but it's easier just to point load in the right direction by addressing the module where the views is located. In your case:

PHP Code:
$this->load->view('module_name/view_name');

// or ..

$this->load->view('templates/header'); 

If you have pass data to the view, it works the normal way like so:

PHP Code:
$data = array();
$data['page_title'] = 'This is the page title';

$this->load->view('templates/header'$data); 

The modules::run() solution Davicotico is referring to is a way to echo the output of a method from a certain module. This is not necessarily needed.

Hope this helps!

-Roger
Reply
#5

Personally, I use a Template library. By using a library, you can avoid the Modules::Run() calls until you really need them, and possibly use the same code (or most of the same code) in a non-HMVC project.

The library I use may serve as an example, but I don't think it will currently work without some supporting code from Bonfire.

The relevant features are the handling of "layouts" and views. Basically, a layout specifies where any number of views will be loaded into a page and defines a content region on the page. The content region is where the current view will be loaded. The current view can be set, but defaults to the view named for the current method (in a directory named for the current module/controller). The library then has a render() method which loads the current view into the content region of the current layout and starts output (so it is typically the last thing called by your controller methods).

All you have to do is load the Template library in any controller which needs it (or in a base controller), and use the Template methods instead of $this->load->view(). In most cases, I'll end up with something like this:


PHP Code:
class Example extends MY_Controller
{
 
   public function __construct()
 
   {
 
       parent::__construct();

        
// Set a layout other than the default 
        // to be used by this controller.
 
       Template::setLayout('no_sidebar');
 
   }

 
   public function index()
 
   {
        
// Load the view from 'example/index.php'
        // into the 'no_sidebar' layout.
 
       Template::render();
 
   }

    public function 
one()
    {
        
// Change the current view to 'example/two.php'
        // instead of 'example/one.php'.
        
Template::set_view('two');
        
        
// Load the current view from 'example/two.php', 
        // but use the layout 'sidebar.php'.
        
Template::render('sidebar');
    }


Of course, there are still cases where $this->load->view() comes in handy, such as loading a view to return to an AJAX request which shouldn't use the template.

I also probably wouldn't use static methods if I were building a library. There doesn't seem to be a significant benefit to it in a CI project.
Reply
#6

Thanks everybody,
Some useful comments here to take on board, some a little complex for my tiny little brain to cope with, I'm new to CodeIgniter and relatively new to PHP, so working through the user guide although in past projects I always built in a module way so to me it makes sense, so off to build a small application to see how it all fits together...

One note I'm struggling with, which I haven't found a solution to either using htaccess or PHP or in the user guide, i've connected to the mysql database and figured how to create a menu based on how the news tutorial using pages/controllers/Pages.php pages/models/Pages_model.php pages/views/index.php & pages/views/views.php so querying the database pages table I can view the individual content on view.php, but how do I get the home page text to pull through from the database on the index.php without using static content??


Regards
Reply
#7

(This post was last modified: 06-04-2015, 08:23 AM by mwhitney.)

http://www.codeigniter.com/user_guide/ge...o-the-view

Load the data in your controller, add whatever you want from the loaded data to an array of key => value pairs, and pass the array to your view. The keys in the array will be converted to variable names which you can use in your view to access that value in your view.

PHP Code:
// Controller
class Example extends MY_Controller
{
 
   public function __construct()
 
   {
 
        parent::__construct();
 
   }

 
   public function index()
 
   {
 
       $dbData $this->db->get('something');
 
       $data = array(
 
           'title' => 'Index Title',
 
           'records' => $dbData,
 
       );

 
       // Now you can use $title and $records in your view 
 
       // to get 'Index Title' and $dbData, respectively.
 
       $this->load->view('index'$data);
 
   }


A template library can include methods to add this data if it calls $this->load->view() for you. For example, Bonfire's Template library uses
Code:
Template::set('variableName', 'value');
Reply




Theme © iAndrew 2016 - Forum software by © MyBB