Page 3 of 4

Re: Object Oriented Blog System Class Structure

Posted: Sun Aug 29, 2010 2:15 pm
by josh
PCSpectra wrote: routing and dispatching, are obviously distinct functionalities,
In your opinion it seems like two responsibilities. You can't just split classes every time you think of it. You'd have an explosion of classes that do nothing. Use your discretion and common sense on when to do it. Your argument is to split it now so I don't have to split it later. Yet it overlooks the fact programmers need to make the opposite operation, merging two or more prematurely split classes back into one.

If it seems like "one thing" it should be also considered to keep them together. Jonah bro if you do decide to split it, that is called a 'refactoring'. http://martinfowler.com/books.html ("extract method" splits long unmanageable methods into multiple smaller ones, and is an example of a refactoring). If you extract a method and change your mind, the reverse operation would be putting it back "inline". http://www.refactoring.com/catalog/inlineMethod.html

Re: Object Oriented Blog System Class Structure

Posted: Sun Aug 29, 2010 9:50 pm
by alex.barylski
I still don't see how it could be split into two functions; It seems like one thing... Do you mean figuring out what module & view need to be loaded, and actually loading them?
Routing is the process of accepting and parsing a some type of input (in this case a URI). How you pass the results to the dispatcher is a matter of choice, whether as an object or an array, whats important, is the router passes enough details to the dispatcher that it can do it's thing, depending on how you want it implemented. In the example below lets assume the actions are methods in a class, which is the controller.

Something very trivial might look like:

Code: Select all

$uri = 'index/index';
dispatcher(router($uri)) 

function router($uri)
{
  list($controller, $action) = explode('/', $uri);
  return array($controller, $action);
}

function dispatcher($route)
{
  include_once("${route[0]}/class.php");  

  $class = $route[0];
  $object = new $class();
  $method = $route[1];
  $object->$method();
}
The functions router() and dispatcher() should be obvious in intent and purpose and the seperatio of concerns is fairly clear, I hope? This is a very basic implementation. In a truly OO MVC framework there are multiple dependencies that can come into play, such as a request object, possibly response and many others.

In this above example with a URI index/index would load a controller named index and invoke a index() method. In side the action is where you actually instantiate the model or views.

Cheers,
Alex

Re: Object Oriented Blog System Class Structure

Posted: Fri Sep 03, 2010 11:13 am
by Jonah Bron
Okay, I did decide to split it up. The controller is now a class called BL_Controller, in /includes/Controller.php . /library/controller.php now just loads the class and runs route() and dispatch().

On the side, I added a function called getIfSet(). It's like isset() on steroids.
josh wrote:Basically another goal is to not nest your if statements/loops too deeply.
I agree, and I reversed most of the IF statements and added more return commands to reduce nesting.

P.S. if I didn't already mention it, I really do appreciate your taking the time to look through my code :mrgreen:

Re: Object Oriented Blog System Class Structure

Posted: Mon Sep 06, 2010 5:25 pm
by Christopher
I looked at the code very briefly. The first thing I noticed was that the framework code was in /includes/ instead of /includes/BL/ or more commonly /library/BL/. That would make the code compatible with almost all autoloaders.

Second was that there was no index.php or bootstrap example. Maybe I missed that earlier in the thread...

Re: Object Oriented Blog System Class Structure

Posted: Mon Sep 06, 2010 8:58 pm
by Jonah Bron
Oops, I forgot to put the index.php file in. It's in a folder called public (also not in the tar). I'll add that. Oh well, it's not too important, because index.php just calls bootstrap.php.

I'll work on rearranging the directory structure. If I put everything in library/BL, where would the files that are in library/ go (bootstrap.php, shared.php, etc.)? Would they just stay there?

Re: Object Oriented Blog System Class Structure

Posted: Mon Sep 06, 2010 10:27 pm
by Christopher
Jonah Bron wrote:Oops, I forgot to put the index.php file in. It's in a folder called public (also not in the tar). I'll add that. Oh well, it's not too important, because index.php just calls bootstrap.php.
Ok ... I'll take a look if I get a chance.
Jonah Bron wrote:I'll work on rearranging the directory structure. If I put everything in library/BL, where would the files that are in library/ go (bootstrap.php, shared.php, etc.)? Would they just stay there?
Typically those files go in an /app/ or /application/ directory that would also include the MVC directories. I assume that bootstrap.php is a customizable application file -- and not part of the framework (in which case it would go into /library/BL/).

Re: Object Oriented Blog System Class Structure

Posted: Tue Sep 07, 2010 9:12 am
by josh
Christopher wrote:
Jonah Bron wrote:I'll work on rearranging the directory structure. If I put everything in library/BL, where would the files that are in library/ go (bootstrap.php, shared.php, etc.)? Would they just stay there?
Typically those files go in an /app/ or /application/ directory .
I'd say in the top 3 most popular frameworks thats how they do it, but I wouldn't call it typical (lots of systems use his structure, ex: Joomla) - I would call your suggestion more organized however (but then again, probably not so organized to someone who works on Joomla as opposed to those other top 3 frameworks).

Re: Object Oriented Blog System Class Structure

Posted: Tue Sep 07, 2010 11:41 am
by Jonah Bron
Christopher wrote:in an /app/ or /application/ directory that would also include the MVC directories.
Wait, let me get a visual. Is this what you're talking about?
  • root/
    • app/
      • library/
        • BL/
          • Auth/
          • Object/
          • Presenter/
      • modules/
      • tpl/
      • bootstrap.php
    • public/
      • index.php

Re: Object Oriented Blog System Class Structure

Posted: Tue Sep 07, 2010 6:41 pm
by Christopher
josh wrote:I'd say in the top 3 most popular frameworks thats how they do it, but I wouldn't call it typical (lots of systems use his structure, ex: Joomla) - I would call your suggestion more organized however (but then again, probably not so organized to someone who works on Joomla as opposed to those other top 3 frameworks).
I cryptically said "typically." I should have said: the current best practice based on major frameworks and the most logical.

This is the rational behind the directory structure I described:
  • root/
    • app/ # this is where application specific code goes
      • controllers/
      • models/
      • templates/
      • views/
      • bootstrap.php
    • library/ # this is where reusable library code goes
      • BL/
        • Auth/
        • Object/
        • Presenter/
      • Zend/
    • public/ # this is where publicly accessible files go
      • images/
      • scripts/
      • index.php

Re: Object Oriented Blog System Class Structure

Posted: Tue Sep 07, 2010 8:55 pm
by Jonah Bron
Thanks for the input so far.

I modified the framework to conform very closely to that layout you gave. I noticed that there was a views/ directory in app/, but there is also Presenter/ directory in library/BL/. Aren't those the same thing? I'm guessing that was a typo and I'm supposed to move the views from the Presenter/ folder to app/views/

Edit: It looks like you mean BL_Presenter_common goes in the Presenter/ directory, and all of the actual views go into the app/views/ dir; is that right?

Re: Object Oriented Blog System Class Structure

Posted: Wed Sep 08, 2010 2:42 am
by Christopher
Jonah Bron wrote:Edit: It looks like you mean BL_Presenter_common goes in the Presenter/ directory, and all of the actual views go into the app/views/ dir; is that right?
Exactly!

Re: Object Oriented Blog System Class Structure

Posted: Wed Sep 08, 2010 12:33 pm
by Jonah Bron
Excellent, I'll update the tar immediately. Just one more thing. What goes inside of app/controllers? Controller.php (the class) or controller.php (calls the class)? Or both?

Re: Object Oriented Blog System Class Structure

Posted: Wed Sep 08, 2010 1:18 pm
by Christopher
app/controllers contains all the Action Controllers for the things your website does like login, products, cart, gallery, etc. They usually map onto the clean URL as mysite.com/controller/method/param1/value1/param2/value2

Re: Object Oriented Blog System Class Structure

Posted: Wed Sep 08, 2010 3:24 pm
by Jonah Bron
This sounds pretty different from the guide I was reading (http://oreilly.com/pub/a/php/archive/mv ... tml?page=1). Obviously you don't want to read the fourteen page article, so I'll summarize. :)

A simple controller takes the URL like this: example.com/module/action/data, loads module (a class), and calls module->action(data).

I took it and warped it a bit. As it is now, the url looks like this: example.com/module/data.view. The module is loaded, and module->__default(data) is called, and it is displayed with the view. __default can do whatever it wants with data. It's just the string between module/ and .view

Could you give a similar summary for what you're describing, because I'm a little blurry?

Re: Object Oriented Blog System Class Structure

Posted: Wed Sep 08, 2010 4:08 pm
by Christopher
What you are calling a "module" is called a Controller in most MVC frameworks that I am aware of. So a Controller is a class that has some methods that can be dispatched -- those methods are called Actions. Hence the term Action Controller. So the URL is "mysite.com/class/method" which is commonly called "mysite.com/controller/action".

The term "module" is usually used in MVC frameworks to mean a named directory that contains a full set of MVC directories (e.g. controllers/, models/, views/). Simple apps do not have modules, just a set of MVC directories in the application/ directory. More complex sites have multiple modules to further separate the code into reuseable parts. The router would be set to identify these. So the URL is "mysite.com/directory/class/method" which is commonly called "mysite.com/module/controller/action".