HMVC the right way ?! |
(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? 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.
(05-28-2015, 12:06 PM)RogerMore Wrote: Thanks guys for your input. 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( Then you can just do PHP Code: $this->load->view('header'); inside your other views. (06-01-2015, 10:49 AM)RogerMore Wrote: And how does that work? 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.
@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? (06-01-2015, 03:12 PM)RogerMore Wrote: Hey Frocco, Thank you (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. 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. |
Welcome Guest, Not a member yet? Register Sign In |