Split the Controller

The default TurboGears skeleton project consists of one controller. I believe most projects would benefit from splitting it into sub-controllers. This lets you separate the logic into their own modules. There are many ways to do the split. I chose the same method used in the Fast Track project.
The code is now easier to work with.

Calling an action in for example the project sub-controller looks like this:

<form action="/project/save" method="post">

The main controller:


...
from subcontrollers.project import ProjectController
from subcontrollers.action import ActionController
from subcontrollers.tag import TagController
from subcontrollers.context import ContextController

class Root(controllers.RootController):
    project = ProjectController()
    action = ActionController()
    context = ContextController()
    tag = TagController()
    ...

Directory structure:
controllers.py
subcontrolles/
-project.py
-action.py
-context.py
-tag.py

About these ads

2 Responses to Split the Controller

  1. fforw says:

    I put all controllers into a controllers package like this:

    controllers/
    -post.py
    -root.py
    -user.py

    The usage of the subcontrollers is the same as shown here, the import a tiny bit different

    from post import PostController

    You also need to change the import of the Root controller in start-prjname.py to

    from prjname.controllers.root import Root

    where prjname is the name of the package of your project

  2. Bader says:

    I rather prefer to put:
    from root import Root
    in __init__.py inside controllers/
    then inside root import them like you
    i think this is better since I don’t need to modify start-PRJNAME.py

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: