0

PREAMBLE: I use F3 Framework (www.fatfreeframework.com).

Let's say I have an abstract class "Controller"

<?php
abstract class Controller
{

    /* F3 */
    protected $f3;
    /* DATABASE SECTION */
    protected $db_name;
    protected $db_host;
    protected $db_username;
    protected $db_password;

    function __construct()
    {
        $f3 = Base::instance();
        $this->f3 = $f3;
    }

    protected function get_db_name()
    {
        return $this->db_name;
    }

    protected function get_db_host()
    {
        return $this->db_host;
    }

    protected function get_db_username()
    {
        return $this->db_username;
    }

    protected function get_db_password()
    {
        return $this->db_password;
    }

    protected function set_db_name($db_name)
    {
        $this->db_name = $db_name;
    }

    protected function set_db_host($db_host)
    {
        $this->db_host = $db_host;
    }

    protected function set_db_username($db_username)
    {
        $this->db_username = $db_username;
    }

    protected function set_db_password($db_password)
    {
        $this->db_password = $db_password;
    }

} // /class Controller

And a FrontendController Class:

<?php
class FrontendController extends Controller
{
    public function get_view()
    {
        $this->set_db_host('localhost');
    }       
}

His work it's manage frontend website (html, print results from database, etc etc etc).

After some day, I need the BackendController class, to manage the news of the site (for example).

1) I need to rewrite the db connections (for example) ? Or I will move them in construct of Controller?

2) In effect, from point 1, I did think to add them in __construct of subclass but ofcourse it override the __construct of abstract Class

3) Tomorrow I want create a ToolboxController Class. Let's say it's scope will be perform some task (e.g. "extract first X characters from a text", "format data according to user settings"), all code pieces that I will/can reuse in other part of app.

In this case I will need to refactor all website changing from:

Controller <= FrontendController

Controller <= BackendController

to

Controller <= ToolboxController <= FrontendController

Controller <= ToolboxController <= BackendController

where <= mean "extends" ?

It seems very very stupid question and easy, but I would understand logic process of add new functions to a born-small website to a large portal.

sineverba
  • 5,059
  • 7
  • 39
  • 84
  • This isn't really the place for "how best to design this application/code base". You might do better on Programmers, but read the rules and regulations first. I'm not saying it's ok there, just it might be, and it's not really suitable here. It's a bit unclear what you are asking as you are asking a fair bit. – James Mar 13 '15 at 11:47
  • Your controller has no business knowing about any database settings. This is for your relevant database abstraction that resides in the *model layer*. So get rid of any database stuff from your controller, it doesn't belong there. – Jimbo Mar 13 '15 at 12:18
  • @jimbo ok... Thank you. And for 2nd question? When project "goes up" and I need add others Controller? – sineverba Mar 13 '15 at 13:17

1 Answers1

2

You do it the Wrong way. The root of your Problem is, you have no Model.

A Model is not a simple class, it as a whole abstraction layer.

  • Domain Object
  • Value Object
  • Database Mapper
  • Service

Read this good answer, to learn about it.

At the end, you Goal should be something like this this

$model = new Model();
$controller = new Controller($model);
$view = new View($model);

The View and the Controller shares the same Model, and they get it. They don't know how to obtain.

Community
  • 1
  • 1
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • Thank you, you spot me right way. But for future addendum? See point 3... Thank you! – sineverba Mar 14 '15 at 11:31
  • For a Database mapper, many people use `Doctrine`. Back in the days, this article helped me a lot, because it is simple practical and not so much theoretical: http://blog.tekerson.com/2008/12/17/data-mapper-pattern-in-php/ – Christian Gollhardt Mar 14 '15 at 14:26
  • @sineverba Sorry my bad, you mean your point 3 :) If you want to work with the data (for example change), you do it via the Service Layer. The Service is called everywhere in you App, you need this. So you encapsulate this logic in a simple `Service::doYouJob($withThisData)`. The example you given about formating, is definitve a thing, which should be in the `View Layer`. To obtain the settings for example, you have a Service in the Model `Service::getCurrentUserSettings()`, which does basicly this: `$mapper = new UserSettingsMapper(); $usersettings = $mapper->findById($user->getId());`. – Christian Gollhardt Mar 14 '15 at 15:05