Welcome Guest, Not a member yet? Register   Sign In
Grouping up functions for controllers and models.
#3

(This post was last modified: 10-24-2019, 02:02 AM by dweenator.)

(10-22-2019, 05:04 PM)daveĀ friend Wrote: At each step of development, the question you need to ask at every turn is, "What is the main concern (purpose) for this class?". Where "class" and "file" are synonymous for our purposes here. In other words, we're talking about the concept called Separation of Concerns (SoC). (Also discussed here which also mentions the closely related topic of the Single Responsibility Principle.)

Using an MVC framework like CodeIgniter helps to maintain SoC to a certain degree. But, as you clearly understand, so does delegating responsibilities to the various pieces and parts. You cannot maintain SoC if you're trying to avoid a "big pile of mess" which I take to mean "a lot of files". The more focused any class is the easier it is to develop, test, use, and maintain. Tightly focused usually means lots of files. (It's not a bad thing - really!)

I tend to think of controllers as content displayed in the browser. IOW, almost every controller method generates output. So both users and products have at least one controller.

Models should be considered in terms of what data they handle and not which controller uses them. Any given model might be used by any number of controllers. There's no reason a controller cannot use more than one model. Come to think of it, there's no reason a model cannot use some other model. (Though that tends to indicate you sould use a common library.)

Speaking of which, don't forget about what CodeIgniter calls "libraries". These are just classes that are put in the /application/libraries/ folder. Many times a "library" will be the piece that provides logic needed by multiple controllers and/or models.

I'm not sure if any of the above fits into your concepts of "Purpose" or "Purpose+Target" but I hope it helps.

What i meant by a big pile of mess is that a single php file(controller/model) having too many functions that are for different specific purposes.

I'll just give out a practical example. I'm developing an website for story sharing online. An example would be www.royalroadl.com where users can browse and read stories at the same time they can also upload their own so there's really no technical difference between a reader and an author.

Anyway, i have a story controller. I have all functions inside them that are related to my story like for publishing a new story, reading a story, bookmarking a story, liking a story, adding a new chapter to a story, i have a function that tracks which chapter a user has last read - last read function(chapter), BASIC CRUD functions.

Anyways, as stated above in my story controller, as much as possible i would like to separate the story functions from the chapter functions. So in short, i want to divide my current story controller into two - story and chapter controller wherein each controller handles their corresponding functions(story and chapter), so basic crud function for story would go into the story controller and basic crud functions for chapters would go into chapter controller.

I also forgot to mention that i still would have to divide the functions from private and public ones, private ones for those functions that require credentials(logged on user - creating a new story, adding a chapter[user must be author of said story], etc.) and public ones for not(like browsing, reading a story, etc.).

Is what i'm asking for logical in this sense? All in all, what i want is a clean project regardless of how many files it has wherein i just have to read their file names and function names for each file and i would already have a grasp of my project.

In essence, i basically just want to organize my project so it is readable. In light of this, i also avoid writing shorthand codes, i prefer the long ones like an if statement i would write out the whole if statement code or if using php in my html i would write the whole php code like <?php echo $name; ?> instead of <?=$name;?>

This is because i have other projects that i completely abandoned because i couldn't get into them after i stopped for awhile and have a look at my project that is messy(in my opinion) that i would rather restart the whole thing from the groundup than to continue them.

I would like to avoid the above situation.

I gave it some thought.

Although chapters are part of the story, if i think about it.

A story is a class of its own with its own attributes. While a chapter is a part of the story, it can also be a class of its own with its own attribute right?( i just remembered my OOP classes with parent class and child classes with inheritance, but this is Codeigniter, so i don't really have to do inheritance and what not, i can just create a different class(controller) for chapters right?
Reply


Messages In This Thread
RE: Grouping up functions for controllers and models. - by dweenator - 10-23-2019, 11:37 PM



Theme © iAndrew 2016 - Forum software by © MyBB