0

私は初心者がPHPの背景に来るので、Pythonとかなり混同しています。

このPython ルーティング ライブラリからルートに設定されているコントローラーを呼び出す方法を理解しようとしています。

たとえば、このようにルートを設定しています。

# Setup a mapper
from routes import Mapper
map = Mapper()
map.connect("add user", "/user", controller = "addUser", action = "add",
  conditions=dict(method=["GET"]))
map.connect("post user", "/user", controller = "postUser", action = "post", 
  conditions=dict(method=["POST"]))
map.connect("update user", "/user/{id}", controller = "updateUser", action = "update", 
  conditions=dict(method=["GET"]))
map.connect("put user", "/user/{id}", controller = "putUser", action = "put", 
  conditions=dict(method=["PUT"]))
map.connect("delete user", "/user/{id}", controller = "deleteUser", action = "delete", 
  conditions=dict(method=["DELETE"]))
map.connect("home", "/", controller = "main", action = "index", conditions=dict(method=["GET"]))

# This is our application object. It could have any name,
# except when using mod_wsgi where it must be "application"
def application(environ, start_response):

   # Get the request path info.
   uri = environ.get('PATH_INFO', '')
   path_info = uri if uri else '/'

   # Get the HTTP request method: PUT, GET, DELETE, POST.
   request_method = environ.get('REQUEST_METHOD', '')
   map.environ = {'REQUEST_METHOD': request_method}

   # Match a URL, returns a dict or None if no match
   mapped = map.match(path_info)

   # Everything done, return the response:
   if mapped['action'] == 'index' :
      response_body = index(environ, start_response, mapped)
   elif mapped['action'] == 'add':
      response_body = addUser(environ, start_response, mapped)
   elif mapped['action'] == 'put':
      response_body = putUser(environ, start_response, mapped)
   elif mapped['action'] == 'delete':
      response_body = deleteUser(environ, start_response, mapped)
   else:
      response_body =  "Not found."

   status = '200 OK'

   response_headers = [('Content-Type', 'text/html'),
               ('Content-Length', str(len(response_body)))]
   start_response(status, response_headers)

   return [response_body]

ご覧のとおり、ルートに設定されているコントローラーはまったく使用されていません。それが一致した場合、if-else条件で個々の関数を呼び出します。これは退屈でつまらない...

これは、私が PHP で行う方法ではありません (おそらく、Python でも行うことはないでしょう)。リクエストからの一致がある場合、ルートがコントローラーを直接呼び出すと予想されるため、それ以上の if-else 条件は必要ありません

以下にこれを持たずに関数controller = "addUser"を呼び出さなければならない方法はありますか?addUser

elif mapped['action'] == 'add':
   response_body = addUser(environ, start_response, mapped)
4

1 に答える 1