ronmarasigan/PIP

Nested Controllers, Helpers and Models

fgarci03 opened this issue · 0 comments

Hello,

I am using this framework to build a slightly-more-than-trivial website, so I need a stronger folder organization.

I implemented a way to handle nested controllers (as in inside folders on the 'controllers' folder) nativelly, with this code:

public function loadController($controller, $action = "index")
{
    require(APP_DIR . "controllers/" . $controller . '.php');

    $controller_name = end(explode("/", $controller));
    $controller = new $controller_name;
    $controller->$action();
}

This, besides calling controllers programmatically, can also handle nested controllers, that will not be accessible through a URL (such as headers, footers, views that depend on others...).

I added it to the controller class, where it fits me, but I guess it could also be a native helper.

I'm not sure if you think this is a good call though. But for the way I like to organize things, this suits me really well.

Do you see any problem with this approach? I tested it and it seems to not break anything, keeping the 'original API' untouched.

P.S. - I also added this to helpers and models in pip.php as they are instanciated with the 'new' keyword. I did not add it to controllers there because it would lead to weird behaviours with the URL and would loose the beautiful simplicity it has.

public function loadModel($name)
{
    require(APP_DIR .'models/'. strtolower($name) .'.php');

    $model_name = end(explode("/", $name));
    $model = new $model_name;
    return $model;
}

public function loadHelper($name)
{
    require(APP_DIR .'helpers/'. strtolower($name) .'.php');

    $helper_name = end(explode("/", $name));
    $helper = new $helper_name;
    return $helper;
}

NOTE: The plugin and view loader already handle this nativelly, because of they way they are implemented.

Thanks!