Welcome Guest, Not a member yet? Register   Sign In
Need to clear up some logic regarding Controllers?
#1

[eluser]Solarpitch[/eluser]
Hey,

Can someone clear this up for me. The way I have been developing my pages is... if for example the site had the following pages...

home
services
about us
contact us

I would have one controller file with a function for each page within this single controller file.

Code:
function index{
//home page
}

function services{
//services page
}

function about_us{
//services page
}


etc..

Should I infact be creating a new controller.php file for each page. So in my controller directory id have..

CONTROLLER
--index.php
--services.php
--about_us.php
--contact.php

Then within each controller file id have my functions that are specific to that page. Index function to load page. Then other functions to handle db results, send mail etc

Code:
//declare class here
//..

function index(

//show view for the page

)
#2

[eluser]davidbehler[/eluser]
I guess it's up to you and depends on how you want your url to look like. Having multiple smaller controllers instead of one big controller might be easier to manage as the code in each controller is cleaner but on the other hand you could have the same code multiple times e.g. if you have a function that's need in more than one controller.

As far as I am concerned there is no general rule, everyone has to decide for himself.
#3

[eluser]Rick Jolly[/eluser]
[quote author="Solarpitch" date="1231635752"]
Should I infact be creating a new controller.php file for each page.
[/quote]
Yes. That is my opinion. Others like to group related pages into the same controller. I like the 1 to 1 controller to view convention because:
1. there is less unnecessary code loaded per request
2. Edit: I find smaller files easier to maintain
3. there is no thinking involved as to what code should go in which controller
4. I know which controller handles which view without looking into the code.

Edit: As for code repetition, I handle that with extending controller classes, and helpers and libraries.
#4

[eluser]Solarpitch[/eluser]
Ah I see, I just wanted to check because I wasn't sure if there was a general rule. I suppose a lot would depend on the scale of the project.

For a brochure site a single controller file might be sufficient but a large web application then I could see why you would use multiple controllers and assign each page to it's own controller.

I didn't want to develop the whole site within a single controller file, then run into problems down the line.
#5

[eluser]Solarpitch[/eluser]
Quote:1. there is less unnecessary code loaded per request
2. Edit: I find smaller files easier to maintain
3. there is no thinking involved as to what code should go in which controller
4. I know which controller handles which view without looking into the code.

Edit: As for code repetition, I handle that with extending controller classes, and helpers and libraries.

Yeah there all good points that make a whole lot of sense! I suppose it would be good to get into the routine of structuring projects in this way so.

As far as models go.. it wouldnt really make much point in then creating a model that is associated with each page. contact_us would have a contact_model and so on. Again I suppose this is a matter of preference.

If for example I had 15 pages (controller files) I could use a single model to handle all db requests by the site (db_model) or just create a model for each page that needed to handle requests to the db.
#6

[eluser]Rick Jolly[/eluser]
Well, models are meant to be shared. Models are defined by related data.

You could create a single model, but then you are loading too much code that isn't well organized and encapsulated. If you create a single model per controller, then you're not able to share that code across multiple controllers and you'll end up repeating code. Don't think of controllers when creating models. Models are just an interface to related data and they could be used/reused by any caller.
#7

[eluser]Solarpitch[/eluser]
Ah right. So if I had a query that needed to insert the same data into a table on 3 pages... I see why it wouldn't make sense if that function was in 3 different models! But rather to have it in defined once in a single model which was accessed by each controller.

Thanks for clearing this up. Smile




Theme © iAndrew 2016 - Forum software by © MyBB