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

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

Closed
videsignz opened this issue Aug 21, 2015 · 5 comments
Closed

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

videsignz opened this issue Aug 21, 2015 · 5 comments

Comments

@videsignz
Copy link

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? :)

@panique
Copy link
Owner

panique commented Aug 22, 2015

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!

@videsignz
Copy link
Author

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.

@panique panique changed the title [Suggestion] Controller Portability (Nested) [Suggestion] Allow usage of controller A inside controller B Oct 11, 2015
@panique panique changed the title [Suggestion] Allow usage of controller A inside controller B [TO CLARIFY] Allow usage of controller A inside controller B Oct 11, 2015
@panique
Copy link
Owner

panique commented Oct 11, 2015

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.

@panique panique closed this as completed Oct 11, 2015
@scriptdevelop
Copy link

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

@scriptdevelop
Copy link

loginwithcookie doesn't work and go in loop

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

3 participants