Welcome Guest, Not a member yet? Register   Sign In
Practical problem in implementing MVC with CodeIgniter
#1

[eluser]Unknown[/eluser]
Hello,

I'm trying to keep this post short so I'll jump right in.
I'm new to MVC/CodeIgniter, currently on the learning process. I have a major issue in how to implement the following:

-webpage with a lot of different modules (e.g.: login, etc.)
-many of the modules have their own submit button and complex behaviour
-many modules will use JavaScripting to simulate drag&drop;, or alerting etc.
-this JavaScript code goes in the View, as it has nothing to do with any content/ data, it simply makes the page easier to interact with/ read; correct?

>> How do I do this in terms of MVC design? <<

I want to implement all modules separately.

This is a problem to me because I know I shouldn't call Controllers from Views, and also I don't want to have one single huge Controller which passes tons of data to a single view, because it will become very hard to maintain/ re-use that code.

I was thinking each module should somehow have a separate View and maybe even Control, but then how do I exactly glue all of those pieces together in CodeIgniter?

Thanks
#2

[eluser]tonanbarbarian[/eluser]
Some MVC frameworks have things called Partials or Elements. These are like mini views that can be included in other view.

I did a quick look on the wiki but cannot find the code example for it used perviously.

Basically you create a new plugin or helper

application/helpers/elements.php
Code:
&lt;?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

function renderElement($view, $params, $display=false) {
  static $CI = null;
  if ($CI===null) $CI =& get_instance();

  $return = $CI->load->view($view, $params, true);
  if ($display)
    echo $return;
  else
    return $return;
} // renderElement()
?&gt;

So this is a very simple element rendering engine. You need to load the helper first of course (in the controller or autoload by preference). Then you simply call renderElement and pass it the name of the view to load, and optionally some params for the view (data variables to create in the view) and whether to display or return the data.

usage in view
Code:
&lt;html&gt;
  &lt;head&gt;
    &lt;?php echo renderElement('headers/head.php'); ?&gt;
  &lt;/head&gt;
&lt;body&gt;
<div class="header">
  &lt;?php renderElement('headers/page.php', array('title'=>$title), true); ?&gt;
</div>
...
&lt;/body&gt;
&lt;/html&gt;

So the first call loads a view in application/views/headers/head.php and must use echo because it is returning the data of the element.
The second call is loading application/views/headers/page.php and passing it a variable called title. It is automatically displaying the element because its display parameter is set to true so it does not need the echo

So using something like this you can modularize your views into elements that can be included wherever needed
use this for JS, CSS, anything you want, even individual image tags or anything that is included in the view
#3

[eluser]tonanbarbarian[/eluser]
Also didnt mention, and was reminded when making another post, that you can use a Layout engine, (search in the forums and wiki) to skin or template your site

Bascially you load the layout library and then tell it the view to load and it places the view within the layout. The layout is just another view used to wrap the content.

Layout
View
Element
Element
Element

And the Layout could load views as well, provided the element helper is loaded of course.
#4

[eluser]Phil Sturgeon[/eluser]
With things like JavaScript and other non-PHP related media/assets you just leave that in a folder in your web folder. For a slightly more CI-feeling asset manager (to load images, javascript, etc) grab the helper from my signature.

Everything else is easy enough. For modules look up Matchbox. Strange name but basically allows a more modular file layout than is currently allowed in CI.

Also, you should not be trying to load a controller from a view. The view is a simple "this variable goes here" file. The controller does the loading and selects what parts and what data goes together. The controller should not be doing huge array formatting either, that sort of work can be left to the model to hide it all away.




Theme © iAndrew 2016 - Forum software by © MyBB