ASP.net MVC と同様の方法でリクエストをルーティングできるようにしたいと考えています。
たとえば、アドレス /Home/Index/query-string/ はリクエストを HomeController にマップし、Index メソッドを呼び出す必要があります。
私はこれを実装して動作しますが、より良い方法を知りたいです。Google App Engine と Python を初めて使い始めたばかりで、ウェブサイトやウェブ アプリケーションの開発経験があまりありません。
これが私の実装です
Yaml ファイル:
- url: /.*
script: router.app
ルーティング構成:
def StartUp():
sys.path.append(os.path.join(os.getcwd(), 'System'))
sys.path.append(os.path.join(os.getcwd(), 'Controllers'))
sys.path.append(os.path.join(os.getcwd(), 'Models'))
app.run()
app = webapp2.WSGIApplication([webapp2.Route('/', handler=Router, name='Default', defaults={'handler': 'home', 'method': 'index', 'query': ''}, build_only=False, handler_method='map'),
webapp2.Route('/<query>', handler=Router, name='DefaultC', defaults={'handler': 'home', 'method': 'index'}, build_only=False, handler_method='map'),
webapp2.Route('/<handler>/', handler=Router, name='DefaultCS', defaults={'method': 'index', 'query': ''}, build_only=False, handler_method='map'),
webapp2.Route('/<handler>/<query>', handler=Router, name='DefaultCA', defaults={'method': 'index'}, build_only=False, handler_method='map'),
webapp2.Route('/<handler>/<method>/', handler=Router, name='DefaultCAS', defaults={'query': ''}, build_only=False, handler_method='map'),
webapp2.Route('/<handler>/<method>/<query>', handler=Router, name='DefaultAll', build_only=False, handler_method='map'),
webapp2.Route('/<handler>/<method>/<query>/', handler=Router, name='DefaultAllS', build_only=False, handler_method='map')],
debug=True)
if __name__ == '__main__':
StartUp()
ルーター
class Router(webapp2.RequestHandler):
def getControllerName(self, handler):
handler = handler.lower()
if(len(handler) == 0):
""" Error - redirect to error page """
return webapp2.redirect('/Error/')
if len(handler) == 1:
handler = handler[0].capitalize()
else:
handler = handler[0].capitalize() + handler[1:]
handler = handler + 'Controller'
return handler
def getActionName(self, method):
method = method.lower()
if(len(method) == 0):
""" Error - redirect to error page """
return webapp2.redirect('/Error/')
if len(method) == 1:
method = method[0].capitalize()
else:
method = method[0].capitalize() + method[1:]
requestType = self.request.method.lower()
requestType = requestType[0].capitalize() + requestType[1:]
return method
def loadController(self, controllerName):
""" Load the controller """
try:
fileName, pathName, description = imp.find_module(controllerName, None)
module = imp.load_module(controllerName, fileName, pathName, description)
# Add checking if class was found
controllerRef = getattr(module, controllerName)
return controllerRef
except ImportError, args:
logging.error('Error importing controller. ' + pprint.saferepr(args))
except Exception, args:
logging.error('Exception. ' + pprint.saferepr(args))
finally:
fileName.close()
def loadAction(self, controller, actionName):
try:
actionRef = getattr(controller, actionName)
except Exception, args:
logging.error('Exception. ' + pprint.saferepr(args))
return actionRef
def callController(self, controllerName, actionName, query):
try:
""" Load the controller """
controllerRef = self.loadController(controllerName)
controller = controllerRef(self, query)
actionRef = self.loadAction(controller, actionName)
actionRef()
except Exception, args:
logging.error('Exception. ' + pprint.saferepr(args))
def map(self, handler, method, query):
query = query.lower()
""" Create Names for Controller and Action"""
controllerName = self.getControllerName(handler)
actionName = self.getActionName(method)
self.callController(controllerName, actionName, query)
これが私のホームコントローラーです:
class HomeController(MVC.Controller):
def Index(self):
path = os.path.join(os.path.dirname('Views/Home/'), 'home.html')
self.Request.response.out.write(template.render(path, None))