Using Methods Of One Controller From Another One

Hi everyone!

I’m trying to write some kind of mini browser-game (not for production (i think so), but for better framework understanding) and i have several global controllers: SiteController, GameController, ForumController etc.

Currently all game-processing functions are placed at GameController, what is, i guess, not good, so i’m looking for some ideas of splitting them to different subcontrollers. But the problem is that everything is running with ajax, so user is permanently placed at /index.php?r=game/index. Navigation and other problems are already solved, so it works fine. (As to views logics - there are several independent parts on the page, that are refreshed separately with calls of, say, game/info or game/items etc.)

So the question is - when the GameController action is called, can i somehow redirect it to, say, WorkController, which will handle some crafting actions and then will renderPartial() its views and pass back to index action of GameController?

Or, may be, there are some better ideas for splitting these functions?

Thanks in advance!

P.S. thanks to Yii team for beta-release of yii2 :)

The best idea is to put your game logic in separate classes which can be reused by all controllers. For example all your controller will initialize the game logic class and will do whatever needs.

pseudo code


class GameLogic() {

// use session to initialize your data or database connection

}

Controler Work {

$gamelogic = new GameLogic();

$gamelogic->craft();

}


Controler Game {


action: refresh{

 $gamelogic = new GameLogic();

 $gamelogic->reload();

}


action: cancel{

 $gamelogic = new GameLogic();

 $gamelogic->cancel();

}


}

In this case all game logic will again appear to be in one place… And my idea is opposite: i want to split it.

I want to make smth like logic of index.php?r=folder/controller/action, i.e. to make folder game with controllers work, battle, movement etc. - if it was not ajax-based app, then i would use such logic, definitely, but the problem is that i can not call it in such way.

Some pointers to guide you:

[list=1]

[*]Use the Pjax widget instead of plain ajax to help generate the complete url on the address bar.

[*]Use prettyUrl in your url manager configuration - for the path structure.

[/list]

Thanks for ideas, i’ve found what i was looking for (don’t know why i haven’t noticed it earlier, it is so simple…):


$this->run('controller/action', $params);

upd.: topic can be closed now :)