Welcome Guest, Not a member yet? Register   Sign In
The right way to make it in mvc
#1

I have an html template, and I'm trying to implement it to ci, I've put the PHP files to view folder. 
Do I need to create a controller and model for every view file?
How can I pass number of vars to one view correctly? 
Thanks!
Reply
#2

See the docs for $this->load->view();

You can load as many views as you need all within the same method.

For instance:


Code:
$data['nested_view'] = $this->load->view('nested', '', TRUE);

$this->load->view('main', $data);


Inside the main view, you can echo $nested_view;
Reply
#3

No, you don't need seperate controllers and models for every view file.
The basic purpose of a controller is handling the web application's logic. The controller's name and its methods play an important role in routing the user to the correct page. In CI, URL's are "segment" based. First segment is the controller's name, second segment is the method's name, and optional following segments are parameters.
So when you see this url:
http://mysite/news/item/45
you may assume that there's a News controller with an Item method inside it that accepts a numeric parameter.

Models are there for getting information from your database, or writing back information to the database.
They are especially useful if you need repeating database operations in various methods in the controller, or across various controllers.

Views are just a way to send html code to the browser.
You can combine multiple views in one method. CI combines them to one output.
E.g.:
PHP Code:
$this->load->view('template/header');
$this->load->view('news/overview'$data);
$this->load->view('template/footer'); 
Reply
#4

There is a special place where you can learn all about Codeigniter, MVC, and so much more .
Its full of wonder and discovery and the joy of learning. Imagine how happy you will be after mastering all the basics of Codeigniter with...
http://www.codeigniter.com/user_guide/tu...index.html
Reply
#5

Can i use one model, controller for all my view files? Like functions.php in wordpress?
Reply
#6

(05-22-2016, 11:03 PM)PlorntXhizors Wrote: Can i use one model, controller for all my view files? Like functions.php in wordpress?


You should use one model per database table,

Controllers should be specific to the task and views should be one view for that task.

If you need a lot of code methods place them into a helper file.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(This post was last modified: 05-23-2016, 08:57 AM by Andy N.)

(05-23-2016, 03:14 AM)InsiteFX Wrote:
(05-22-2016, 11:03 PM)PlorntXhizors Wrote: Can i use one model, controller for all my view files? Like functions.php in wordpress?


You should use one model per database table,

Controllers should be specific to the task and views should be one view for that task.

If you need a lot of code methods place them into a helper file.

Pretty much what he said.  If you try to lump everything into one model and one controller you're missing out on much of the benefits of the MVC approach.

The way I use MVC is that Models normally represent 'objects' (for example "Customers") which relate to one or sometimes multiple tables (for example "Orders" will typically have an order header table, and an associated order lines table, but you would only want one model).  The model provides a unified, re-usable way to interact with those objects - the most common cases being providing methods to create, read, update and delete them, aka CRUD.   They can also implement business logic.

Controllers typically encapsulate the processes / logic which defines the application, or at least that part of it which interacts with the user.  

Views define the user interface, and should be as 'dumb' as possible.

Some golden rules (or at least, rules I tend to follow):

- Views should be as logic free as possible, maybe some foreach loops and if statements but mostly just HTML with interpolated variables.
- Controllers should never interact directly with the data, that's what models are for.
- Sometimes it's not clear whether logic should be in the Model or the Controller. In general I would advise that logic that is specific to one use-case should be in a controller, whereas logic which is likely to be reused should be in a model.  

If you are trying to do something truly simple and disposable, you can just not bother with a model and do everything with just views and a controller.  Though I would personally tend to have a model just for the sake of good form.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB