Welcome Guest, Not a member yet? Register   Sign In
HMVC the right way ?!
#31

(06-01-2015, 10:25 PM)RogerMore Wrote: So you have your own code which you can put from 1 project to another which is called libraries. Who doesn't have that. One thing I'm missing though, is that your auth lib doesn't seem to need models and views for login/password forget screens or your payment gateway doesn't need at least some views for choosing which payment and so on. I hope that your login screen isn't popping out of your auth lib because I don't know if that would be a best practice...

What are you talking about?   Huh

A library is a block of code that facilitates certain functions to be exposed for the Controller or to pass data to/from the Model.  I have libraries which then can be extended should that particular project need it.  Or I can just use minimal functions because the project is simplistic.  To be clear, the Libraries I have are not written in Codeigniter (so they are framework agnostic).  They are "included" and then I use whatever endpoints I need to.  The future endeavor is to use namespaces and then include composer support.  So then I autoload them and exist out of the structure of the framework.  But then thats out of the scope of this discussion.

After I have completed the integration of the library.  The controllers are updated, again very trivial, the database is updated and the models written (again very trivial) and whatever views I want to use for the project are used.

All this work, literally 5 to 10 minutes work of dropping in a specific functionality.  In future, when I have the code in composer.  It's just a case of changing the composer file.  Doing an update and then hitting the ground running.  Like I am doing with the several composer modules I'm using now.
Reply
#32

(05-28-2015, 12:06 PM)RogerMore Wrote: Thanks guys for your input.

Because of the problems controllers calling controllers 'may' cause, I'm going to get rid of all the




PHP Code:
echo modules::run("module/controller"); 
stuff!

I have a good idea how to get the solution which will give me the same output, without doing things to CI which is frowned upon.  Tongue
And I will definitely try out the Bays addon... thanks kilishan! 

-Roger

I usually set up a MY_Controller in which i make use of $this->load->vars(), to load an array with a set of variables that will be made available for all views. My other controllers will inherit from MY_Controller and the views they load will have access to whatever i loaded on $this->load->vars(). 

PHP Code:
$this->load->vars(
'var_for_all_views' => 'some value',
'other_var_for_all_views' => 'some other value',
); 

Then you can just do

PHP Code:
$this->load->view('header'); 

inside your other views.
Reply
#33

(06-01-2015, 10:49 AM)RogerMore Wrote: And how does that work? 

Loading the news model from my pages controller and calling one of it's methods which calls a method of my news library..?! 
An example would be really helpfull to explain it to me.

Thanks in advance.

-Roger

@frocco, I didn't forget you. I'll post the answer to your question tonight or tomorrow.

When you load a library, you can pass an array of parameters to the library's constructor:

Code:
$this->load->library('libraryName', array('param1' => $data1, 'param2' => $data2));

Alternatively, you can just pass the data to the library's methods when you use them.
Reply
#34

(This post was last modified: 06-03-2015, 01:55 AM by RogerMore.)

@no1youknowz: Thanks for the explanation. 

Now I have an idea how you work. You should take into account though that when you write 'It's called libraries' without further explanation people think CI libraries. Don't expect that people automatically think outside CI and that you are referring to scripts you are including somewhere into your projects.

@josetrindade: Thanks for your input. 

This is another way to get de data to my page template, which is nice. But it's not really a solution to my question which is a better way to get data output from another HMVC model without using the modules::run method.

@mwhitney: Ok, so it's better to load the news model and news library from my page controller. 
Then model retrieves the data and I give it to the library to process it into data for a view?
Reply
#35

(06-01-2015, 03:12 PM)RogerMore Wrote: Hey Frocco,

My project isn't anywhere near finished and I'm ripping my code apart to take the modules::run out, so bear with me when I try to simplify my code.

A page in my cms has sections like header, left, middle, right, footer. Every section can hold 1 or more elements like a text or menu view. A page also has one template with the section predefined in it.

Supersimple page template:

Code:
<!DOCTYPE html>
<html>
<head>
   <style>
   </style>
</head>
<body>

   <div id="header">
   <?php if (isset($header)) echo $header; ?>
   </div>
   
   <div id="left">
   <?php if (isset($left)) echo $left; ?>
   </div>
   
   <div id="middle">
   <?php if (isset($middle)) echo $middle; ?>
   </div>
   
   <div id="right">
   <?php if (isset($right)) echo $right; ?>
   </div>
   
   <div id="footer">
   <?php if (isset($footer)) echo $footer; ?>
   </div>

</body>
</html>
I have one page controller which matches a slug to a page in the database and finds out which elements go with which sections. That data is reformatted into something that looks like:

Code:
Array
(
   [header] => Array
       (
           [10] => Array
               (
                   [module] => html
                   [view] => test
                   [settings] =>
               )

           [20] => Array
               (
                   [module] => flexslider
                   [view] => content
                   [settings] =>
               )

       )

)

Here you can see that there are 2 elements found for section header. In a foreach loop the output for the section is created:


PHP Code:
foreach ($elements as $section => $element) {
 foreach (
$element as $block) {
 
// here should be a check if a certain library is already loaded
 // ..
 
$this->load->library($block['module'].'/'.$block['module']); // this loads the html or flexslider library from the module with the same name
 
 
$html $this->$block['module']->$block['view']($block['settings']); // this call the method test from the html or the method content from the library flexslider which will create the output for the views.

 
if (!isset($sections[$section])) $sections[$section] = '';
 
$sections[$section] .= $html;
 }


After this all HTML for all sections is created and the array of $sections can be the data for the page template.


PHP Code:
$this->load->view("templates/{$pageTemplate}"$sections); 

And boom, your page with all the good stuff is put to screen...

Hope this helps.

-Roger

Thank you
Reply
#36

(06-03-2015, 12:40 AM)RogerMore Wrote: Ok, so it's better to load the news model and news library from my page controller. 
Then model retrieves the data and I give it to the library to process it into data for a view?

In most cases this is true. You generally want each class to be as simple as possible, especially since this often makes them more reusable.

For a simplified example, I could build a library which calls my model to retrieve some data, then creates a select element (drop-down), using one column for the values and another for the content/displayed value. However, this would tie the library to the model (and possibly even to the columns), and the library would know far more about the data than is really necessary for this functionality.

If I instead built my library to expect the data and column names to be supplied as needed, the library could be used to transform any dataset into a select element. If the library contains a series of methods for performing transformations on data, you may decide that the data to be transformed should be passed to the constructor, while any additional information required for the transforms (like the column names for the drop-down) could be passed to the methods.

Of course, this example is simple enough that much of the required functionality is built into many base models, but I hope it helps to illustrate the point.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB