Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Controllers, Helpers and Models #42

Open
fgarci03 opened this issue Jan 7, 2016 · 0 comments
Open

Nested Controllers, Helpers and Models #42

fgarci03 opened this issue Jan 7, 2016 · 0 comments

Comments

@fgarci03
Copy link

fgarci03 commented Jan 7, 2016

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant