panique/huge

[TO CLARIFY] Allow usage of controller A inside controller B

videsignz opened this issue · 5 comments

Only working with HUGE for a couple weeks, during some implementation I ran across the need to copy some functions from one controller to be able to use in another. I decided to create a function that would make this easier.

OBJECT->METHOD UPDATED 8/22/2015

This one function is added to the main Controller class

public function UseController($controller, $method){
    $controller = $controller.'Controller';
    require Config::get('PATH_CONTROLLER') . $controller.'.php';
    $this->newController = new $controller();
    $this->newController->$method();
}

And to use within a Controller (ie. UserController)

public function showProfile() {
        $this->UseController('Login', 'showProfile');
}

STATIC::METHOD Updated 8/22/2015
This may be easier on the eyes when using...pure preference though.

This one function is added to the main Controller class

public function method($controller, $method){
    $controller = $controller.'Controller';
    require Config::get('PATH_CONTROLLER') . $controller.'.php';
    $this->newController = new $controller();
    $this->newController->$method();
}

And to use within a Controller (ie. UserController)

public function showProfile() {
        Controller::method('Login', 'showProfile');
}

Not sure if this is something that others feel should be a part of the framework but like I said, it has helped me out tremendously, especially when I split 'processing' functions from 'view' functions into separate controllers for easier maintenance. Surely that is not the only use case though.

Thoughts anyone? :)

Interesting! By the way other "real" frameworks do this via static or pseudo(?)-static calls, like ProfileController::getProfiles(), I'm currently doing this with YII2 quite often. They include these controllers etc. via use \namespacePath\to\ProfileController.

In HUGE, when wanting to call some functions several times I'm usually creating static methods inside a new class in /core folder, like the Filter class for example, and then call the method everywhere inside the project via Filter::doSomething().

The reasons is that calling a controller from another controller feels a little bit weird and inaccurate, i remember some issues when reading into proper MVC, even I dot this by myself every day :) in Yii framework.

I find your idea quite clever, so is it okay if I put it inside the project ? Feel free to submit a pull request if you like!

Thanks a lot!

Hi @panique I would be honored if this was part of the project! I just added a static method as well if you feel that would be better :)

Also, I am new to GitHub (Joined the 17th of this month, ha ha) and am not sure what a pull request entails but I would love to take a stab at it!

I am a quick learner so if you have want to point me in the right direction, that would be great.

He, I've linked this ticket from the main README, but I'll close this ticket for now (hope that's okay for you). Let's reopen this when there's a bulletproof solution or pull request.

Hy panique, i m desperate , because the ' header location and redirects do not work ? Help me if you can . Congratulations on the job

loginwithcookie doesn't work and go in loop