开发者

Is it better to send XMLHttpRequests to different scripts or one master script that delegates tasks?

I am building a r开发者_运维百科ather larger web application with javascript and PHP. The app has several different types of XMLHttpRequests, and my question is about best practice: is it better to send each of those requests to a different PHP script or to one master script which then goes through and delegates tasks?

Currently I have a requestManager script, but it's getting a bit out of hand. I feel like it's nice to have all of my requests hit that same script first because It's easy for me to debug and remember where my requests are going. The problem is that I'm looking at around 10 if-then statements, and I can imagine that might start to slow things down as it gets bigger and bigger.


The single point of entry to your application should be some kind of router, which directs requests to the correct controller (script in your case). It sounds like you're reinventing the wheel, and poorly. Your router should not be a big series of if/else statements; you should store a mapping of URI patterns to controllers and do a single lookup.

You should also be a lot less worried about performance, which doesn't matter at all, and more worried about producing a completely unmaintainable piece of code.

If you're truly concerened with best practices, stop what you're doing and build your "rather large" web application with a PHP framework where routing is done for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜