I am using Kohana but this question applies to Rails, CI, or any other MVC web development framework. Where is the best place to stick one's server side AJAX scripts?
我正在使用Kohana但这个问题适用于Rails,CI或任何其他MVC Web开发框架。坚持一个服务器端AJAX脚本的最佳位置在哪里?
I was planning on creating an Ajax_Controller and using a method/action per individual script.
我计划创建一个Ajax_Controller并使用每个脚本的方法/操作。
For example, a login form on the home page index.php/home
would send an XMLHttpRequest to index.php/ajax/login
, and the edit profile form index.php/profile/edit
would send an XMLHttpRequest to index.php/ajax/editprofile
. What's the best practice?
例如,主页index.php / home上的登录表单会向index.php / ajax / login发送XMLHttpRequest,编辑配置文件表单index.php / profile / edit会向index.php / ajax发送XMLHttpRequest。 /编辑个人资料。什么是最佳做法?
9 个解决方案
#1
1
I don't use Kohana but what I do in my framework is that AJAX scripts are controllers. I try to treat them as standalone controllers but in the end they are just controllers.
我不使用Kohana,但我在我的框架中所做的是AJAX脚本是控制器。我尝试将它们视为独立控制器,但最终它们只是控制器。
#2
9
I tend to put my ajax actions in the same controller as the non-ajax actions for any given model.
我倾向于将我的ajax动作放在与任何给定模型的非ajax动作相同的控制器中。
When I can, I try to use the same actions and only change the output type. Most tasks should have a non-ajax version anyway, so this tends to work quite well. Very handy for reducing logic duplication.
当我可以,我尝试使用相同的操作,只更改输出类型。大多数任务都应该有一个非ajax版本,所以这往往很好。非常方便减少逻辑重复。
#3
7
AJAX crosses all of the MVC boundaries. That is, it doesn't go into just one of model, view or controller.
AJAX跨越了所有MVC边界。也就是说,它不仅仅涉及模型,视图或控制器之一。
- Your AJAX scripts will be calling scripts on your site - so this would involve a section of your controller layer which you've created for the purpose.
- 您的AJAX脚本将调用您站点上的脚本 - 因此这将涉及您为此目的创建的控制器层的一部分。
- That controller in turn would access the database using the interface provided by your model layer, just as a non-AJAX request would.
- 反过来,该控制器将使用模型层提供的接口访问数据库,就像非AJAX请求一样。
- The data for the response back to the client may be packaged as JSON or XML or something. Technically this is the task of your view layer, though if your application's definition of a view layer is nothing more than "an HTML templating system" rather than "processing and formatting anything that gets sent back to the client whether it's HTML or something else like XML" then your XML or JSON generation may need to go into a new little section of its own.
- 返回客户端的响应数据可以打包为JSON或XML等。从技术上讲,这是视图层的任务,但是如果您的应用程序对视图层的定义只不过是“一个HTML模板系统”而不是“处理和格式化任何被发送回客户端的内容,无论它是HTML还是其他类似的东西” XML“那么你的XML或JSON生成可能需要进入它自己的一个新的小部分。
As for sending the scripts (Javascript files) themselves, this is probably going to be handled directly by the web server rather than from within your MVC framework.
至于自己发送脚本(Javascript文件),这可能是由Web服务器直接处理,而不是在MVC框架内处理。
#4
5
Do you make different controllers for GET and POST requests? I don't. In my opinion, JS requests shouldn't be dealt with differently either.
你为GET和POST请求制作了不同的控制器吗?我不。在我看来,JS请求也不应该以不同的方式处理。
I personally see JS requests just like GET, POST or any other type of request. So if I have user-related JS-based actions, I simply create them in the user controller.
我个人看到JS请求就像GET,POST或任何其他类型的请求。因此,如果我有与用户相关的基于JS的操作,我只需在用户控制器中创建它们。
#5
2
If you mean the AJAX (Javascript) scripts themselves, these should go into your public/js folder. However, if you mean the actions invoked by these AJAX requests, they should be treated as any other actions of the respective controllers. To be completely RESTful, you should be using a different format (json, xml, etc.) as return values for those actions.
如果你的意思是AJAX(Javascript)脚本本身,它们应该进入你的public / js文件夹。但是,如果您指的是这些AJAX请求调用的操作,则应将它们视为相应控制器的任何其他操作。要完全RESTful,您应该使用不同的格式(json,xml等)作为这些操作的返回值。
#6
2
I am a noob, but based on my understanding, to achieve ajax with php mvc... thinking steps might be:
我是一个菜鸟,但根据我的理解,用php mvc实现ajax ...思考步骤可能是:
- change the definition/function of the existing php view layer from 'HTML template' into 'results formatting (XML,JSON etc..' -> results from relevant module, which then called by controller to output into AJAX object, then it means you need to write view layers into each particular class with formatting methods
- 将现有php视图层的定义/功能从'HTML模板'更改为'结果格式化(XML,JSON等......) - 相关模块的结果,然后由控制器调用以输出到AJAX对象,那么它意味着你需要使用格式化方法将视图层写入每个特定的类
- PHP module layer stays same
- PHP模块层保持不变
- build a Ajax router class with JS which stay the same structure which you route in your PHP
- 使用JS构建一个Ajax路由器类,它保持与您在PHP中路由的结构相同
- build a ajax results handler class with JS to handle the results got back from PHP controllers (XML JSON etc..), then from here do whatever user interactions you want, this will be called by above Ajax router class
- 使用JS构建一个ajax结果处理程序类来处理从PHP控制器(XML JSON等等)返回的结果,然后从这里做任何你想要的用户交互,这将由上面的Ajax路由器类调用
So,
所以,
ajax router (send XMLhttprequest)
-> PHP controllers C
-> PHP module -> PHP view results M
-> PHP controllers output results V
-> ajax results handle (into page)
#7
1
Using a separate controller is a good idea. I either organize my controllers by function and then actions by return type.
使用单独的控制器是个好主意。我要么按功能组织我的控制器,要么按返回类型组织操作。
Additionally, when I'm using Pylons I can decorate an action with @jsonify and that will automatically take care of converting python objects to JSON. Very handy.
另外,当我使用Pylons时,我可以使用@jsonify修饰一个动作,并自动将python对象转换为JSON。非常便利。
#8
1
I like to keep all my ajax requests in one controller, typically dispatching their requests through a shared model (that the non ajax controller also uses)
我喜欢将所有的ajax请求保存在一个控制器中,通常通过共享模型(非ajax控制器也使用)调度它们的请求
The main difference being the view that results via the ajax controller (html fragments, json data, etc) or the non-ajax controller (full pages)
主要区别是通过ajax控制器(html片段,json数据等)或非ajax控制器(完整页面)生成的视图
#9
0
You could wrap it up as a general REST-api, and use RESTful conventions and URIs. Example:
您可以将其包装为常规REST-api,并使用RESTful约定和URI。例:
Instead of index.php/ajax/editprofile it could be a PUT request to index.php/api/profile/profilename.
它可能是对index.php / api / profile / profilename的PUT请求,而不是index.php / ajax / editprofile。
#1
1
I don't use Kohana but what I do in my framework is that AJAX scripts are controllers. I try to treat them as standalone controllers but in the end they are just controllers.
我不使用Kohana,但我在我的框架中所做的是AJAX脚本是控制器。我尝试将它们视为独立控制器,但最终它们只是控制器。
#2
9
I tend to put my ajax actions in the same controller as the non-ajax actions for any given model.
我倾向于将我的ajax动作放在与任何给定模型的非ajax动作相同的控制器中。
When I can, I try to use the same actions and only change the output type. Most tasks should have a non-ajax version anyway, so this tends to work quite well. Very handy for reducing logic duplication.
当我可以,我尝试使用相同的操作,只更改输出类型。大多数任务都应该有一个非ajax版本,所以这往往很好。非常方便减少逻辑重复。
#3
7
AJAX crosses all of the MVC boundaries. That is, it doesn't go into just one of model, view or controller.
AJAX跨越了所有MVC边界。也就是说,它不仅仅涉及模型,视图或控制器之一。
- Your AJAX scripts will be calling scripts on your site - so this would involve a section of your controller layer which you've created for the purpose.
- 您的AJAX脚本将调用您站点上的脚本 - 因此这将涉及您为此目的创建的控制器层的一部分。
- That controller in turn would access the database using the interface provided by your model layer, just as a non-AJAX request would.
- 反过来,该控制器将使用模型层提供的接口访问数据库,就像非AJAX请求一样。
- The data for the response back to the client may be packaged as JSON or XML or something. Technically this is the task of your view layer, though if your application's definition of a view layer is nothing more than "an HTML templating system" rather than "processing and formatting anything that gets sent back to the client whether it's HTML or something else like XML" then your XML or JSON generation may need to go into a new little section of its own.
- 返回客户端的响应数据可以打包为JSON或XML等。从技术上讲,这是视图层的任务,但是如果您的应用程序对视图层的定义只不过是“一个HTML模板系统”而不是“处理和格式化任何被发送回客户端的内容,无论它是HTML还是其他类似的东西” XML“那么你的XML或JSON生成可能需要进入它自己的一个新的小部分。
As for sending the scripts (Javascript files) themselves, this is probably going to be handled directly by the web server rather than from within your MVC framework.
至于自己发送脚本(Javascript文件),这可能是由Web服务器直接处理,而不是在MVC框架内处理。
#4
5
Do you make different controllers for GET and POST requests? I don't. In my opinion, JS requests shouldn't be dealt with differently either.
你为GET和POST请求制作了不同的控制器吗?我不。在我看来,JS请求也不应该以不同的方式处理。
I personally see JS requests just like GET, POST or any other type of request. So if I have user-related JS-based actions, I simply create them in the user controller.
我个人看到JS请求就像GET,POST或任何其他类型的请求。因此,如果我有与用户相关的基于JS的操作,我只需在用户控制器中创建它们。
#5
2
If you mean the AJAX (Javascript) scripts themselves, these should go into your public/js folder. However, if you mean the actions invoked by these AJAX requests, they should be treated as any other actions of the respective controllers. To be completely RESTful, you should be using a different format (json, xml, etc.) as return values for those actions.
如果你的意思是AJAX(Javascript)脚本本身,它们应该进入你的public / js文件夹。但是,如果您指的是这些AJAX请求调用的操作,则应将它们视为相应控制器的任何其他操作。要完全RESTful,您应该使用不同的格式(json,xml等)作为这些操作的返回值。
#6
2
I am a noob, but based on my understanding, to achieve ajax with php mvc... thinking steps might be:
我是一个菜鸟,但根据我的理解,用php mvc实现ajax ...思考步骤可能是:
- change the definition/function of the existing php view layer from 'HTML template' into 'results formatting (XML,JSON etc..' -> results from relevant module, which then called by controller to output into AJAX object, then it means you need to write view layers into each particular class with formatting methods
- 将现有php视图层的定义/功能从'HTML模板'更改为'结果格式化(XML,JSON等......) - 相关模块的结果,然后由控制器调用以输出到AJAX对象,那么它意味着你需要使用格式化方法将视图层写入每个特定的类
- PHP module layer stays same
- PHP模块层保持不变
- build a Ajax router class with JS which stay the same structure which you route in your PHP
- 使用JS构建一个Ajax路由器类,它保持与您在PHP中路由的结构相同
- build a ajax results handler class with JS to handle the results got back from PHP controllers (XML JSON etc..), then from here do whatever user interactions you want, this will be called by above Ajax router class
- 使用JS构建一个ajax结果处理程序类来处理从PHP控制器(XML JSON等等)返回的结果,然后从这里做任何你想要的用户交互,这将由上面的Ajax路由器类调用
So,
所以,
ajax router (send XMLhttprequest)
-> PHP controllers C
-> PHP module -> PHP view results M
-> PHP controllers output results V
-> ajax results handle (into page)
#7
1
Using a separate controller is a good idea. I either organize my controllers by function and then actions by return type.
使用单独的控制器是个好主意。我要么按功能组织我的控制器,要么按返回类型组织操作。
Additionally, when I'm using Pylons I can decorate an action with @jsonify and that will automatically take care of converting python objects to JSON. Very handy.
另外,当我使用Pylons时,我可以使用@jsonify修饰一个动作,并自动将python对象转换为JSON。非常便利。
#8
1
I like to keep all my ajax requests in one controller, typically dispatching their requests through a shared model (that the non ajax controller also uses)
我喜欢将所有的ajax请求保存在一个控制器中,通常通过共享模型(非ajax控制器也使用)调度它们的请求
The main difference being the view that results via the ajax controller (html fragments, json data, etc) or the non-ajax controller (full pages)
主要区别是通过ajax控制器(html片段,json数据等)或非ajax控制器(完整页面)生成的视图
#9
0
You could wrap it up as a general REST-api, and use RESTful conventions and URIs. Example:
您可以将其包装为常规REST-api,并使用RESTful约定和URI。例:
Instead of index.php/ajax/editprofile it could be a PUT request to index.php/api/profile/profilename.
它可能是对index.php / api / profile / profilename的PUT请求,而不是index.php / ajax / editprofile。