Welcome Guest, Not a member yet? Register   Sign In
mvc all in one file
#1

[eluser]jplanet[/eluser]
I need to create some modules for an ecommerce application, such as "Welcome username!" and "Cart: 3 Items", which will appear on every page throughout the website.

these items require a query to obtain the data to display, code to assign the results to a variable in CI's $data array, and a small view file to display it.

Going the traditional Codeigniter route, this would involve adding code to every single Controller function. This seems counter-intuitive and error-prone.

Is there a way, in the Codeigniter architecture, to have a component which contains all database, logic, as well as display code? I'm thinking of someting I can simply include in as a view 'part', but that contains all of the code necessary to display.

I am almost tempted to just write a traditional PHP script to include, but I need to access CI's session functions for the above mentioned functionality.

Any help is greatly appreciated!
#2

[eluser]Matthew Lanham[/eluser]
Have you looked into Models and Libraries....
#3

[eluser]jplanet[/eluser]
[quote author="swanweb" date="1206405826"]Have you looked into Models and Libraries....[/quote]

That was one of the first places I looked, although it didn't seem to work well for the
view" portion.

Hmmmm...if I can call a model directly in a view, that might be the ticket...
#4

[eluser]Matthew Lanham[/eluser]
Well technically you can, however i wouldn't recommend it (not really MVC style), you should call it in the controller and pass the data to the view....which will result in the same output, but much cleaner...
#5

[eluser]Pascal Kriete[/eluser]
What I would do here is to place it all in the global controller:

libraries/MY_Controller.php:
Code:
class BaseController extends Controller {
    function BaseController()
    {
        parent::Controller();

        //load model, do operations, etc
        $data['bla'] = stuff;
        $this->load->view('header');
    }
}

Then you make all your Controllers extend BaseController instead of just Controller. And voila, that code get's called for all of them.

Also, as long as you only get data from the model in a view, it is perfectly acceptable MVC. The view just shouldn't write to the model.
#6

[eluser]wiredesignz[/eluser]
You can access the Model from your View (its is perfectly ok MVC style) However you may not modify the Model state from your View.

The only caveat is that you are careful not to make the View dependent on any specific Model(s).

Passing a reference to the Model into the View can avoid dependency.

EDIT:
inparo is too fast for me. Wink
#7

[eluser]beemr[/eluser]
I've been able to treat my login component library as if it were a view. The multiple views addition in 1.6 made it possible for me to pass membership requirements in to the library function and have it return either the appropriate view for the user's state or reject them. Just make sure that your main library function returns $this->CI->load->view('loginview.php',$data) when it resolves to true. Upon a false, you can call a rejection routine. This way each controller's view setup might look like this:
Code:
$this->load->view->header.php;
$this->MY_library->login_component($this->MY_library->check('designer'));
$this->load->view->footer.php
In this way, MY_library->check() will return true if the current user is at least a 'designer,' and upon true, MY_library->login_component will build the appropriate view call.




Theme © iAndrew 2016 - Forum software by © MyBB