9

I have a pretty large site, and I am looking for the most time efficient way to manage it (I am the sole coder).

I am trying to devise a very simple MVC structure (i don't want to use a framework) to help keep all my code in order.

For a huge site, is it better to have only one controller to handle all the pages, or is it better and easier to split them up?

If just one, what is a good example of a non-framework controller?

johnnietheblack
  • 13,050
  • 28
  • 95
  • 133
  • 1
    Why do you want to avoid a framework? While some are restrictive (do it *this* way, or you'll have to hack it to death), there are some that are pretty flexible (pick and choose what you want). – Tim Lytle Sep 29 '09 at 00:08
  • 2
    well, i mainly want to avoid because i have the freedom and time to implement my own, and I'd really like to have a ground up experience with one before i start using a framework. also...i dont want more than i need, in general – johnnietheblack Sep 29 '09 at 00:10

5 Answers5

4

I would split any logical divisions into different controllers - if it's all static pages, then serve it up with all the same 'static page' controller.

If you have some static pages, a FAQ page (or section), a product list - use a controller for each different section. So the static pages would be pulled from flat files or a database by one controller, the FAQ pages would be generated from a FAQ table by another controller, the products and info would be generated by whatever the source for that is.

Every time the way a page is generated or the data is accessed, use a different controller.

Of course, class inheritance can be used to create the base class with the code needed by any controller.

Not sure what you mean by a non-framework controller - I'd checkout the Zend (gasp) 'Framework', the MVC pattern, and even the controller itself, can be used apart from the rest of the framework.

Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
4

I tend to split the controllers up based on their responsibility to a specific section of a site/application. This makes maintaining the code so much easier. Furthermore, I group controllers (and views, models) within modules (folders). Here's an example from a current project I'm working on:

  • Blog
    • Posts
    • Comments
    • Categories
  • Settings
    • Posts
    • Users

The more complex a site, the more modules I use. Although most of my modules only contain one 'Index' controller, I do like the organization they provide.

Then I use a router (front controller) that maps a REST style URI to the proper module / controller / action. Ex: mysite.com/blog/posts/view/7 would call Controller_Posts::view(7) from the "blog" module. An added benefit of using modules is I can have more specific URIs than if I didn't have modules. Although I suppose that could be remedied by using a router that supports defining custom routes, but I'm not too fond of that.

As many other things, it boils down to what you're comfortable with as a developer, but we can probably agree that more organization you have, the better off you are, so long as you don't over complicate things.

As a quick aside, I would recommend you look into using a framework. I understand if you don't want to use one of the ones out there already, as I avoided those too. I ended up writing my own which for the past year has served me very well. It was a great learning experience and it only contains what I want / need. That being said, you might want to look into Kohana and CakePHP - they're not overly bloated IMO and they will definitely save you time should you decide not to write your own.

Steven Mercatante
  • 24,757
  • 9
  • 65
  • 109
3

Typically people split up controllers into controllers focused on specific areas of functionality.

Then they stick a "front-controller" in front of it all, so there's only one entry point to the application. The front-controller's only job is to route incoming requests to the appropriate controller.

Look at the way the Zend_Controller component is set up. It may provide everything you need, and you're free to use it without buying into the complete Zend Framework.

timdev
  • 61,857
  • 6
  • 82
  • 92
1

It depends how you other parts will work. If you only have one model file then it's probably not worth splitting up the controller. If you can split up the model into sections as well as the controller, then do that.

However, I often find that there's too much overlap between models to separate them. You might have a model for articles, but if you want to display the top 20 articles in a sidebar on other pages, that code should be in the article model - and you're going to need that on every page.

Honestly though, the only way to do it is try it and see. Start with a single entry point, and if it gets too, unwieldy, refactor it into smaller chunks.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
1

One router/dispatcher, many controllers would be my advice. Controllers ought to map to URLs, which means differing functionality. A controller will collaborate with different services to accomplish each use case, so one controller for your entire app would become too unwieldy if your app has more than a handful of use cases.

duffymo
  • 305,152
  • 44
  • 369
  • 561