将ajax选项卡与MVC PHP项目一起使用时的最佳方法和问题

时间:2022-10-21 11:23:57

I'm building an IMDB.com like website using PHP/jQuery and a MVC approach (no OOP).

我正在使用PHP / jQuery和MVC方法(没有OOP)构建一个类似网站的IMDB.com。

I have an index.php base controller to 'rule them all' :), a controllers folder with all the controllers, a models folder and a view folder.

我有一个index.php基本控制器来“统治所有”:),一个包含所有控制器的控制器文件夹,一个模型文件夹和一个视图文件夹。

In some pages of the website I have tabbed navigation, when the visitor clicks on one of those tabs to get more information, jQuery gets that data using the $.post or $.get method and shows it on the tab container, obviously without refreshing the page.

在网站的某些页面中我有标签导航,当访问者点击其中一个标签以获取更多信息时,jQuery使用$ .post或$ .get方法获取该数据并在标签容器上显示,显然没有刷新这一页。

The problem is that those pages loaded by ajax are also generated using controllers, models, and views, and the things are getting a bit complicated for someone like me ( = 'no experience'). To dynamically get the data I some times need to include a model twice, include an include in an include in an include, send information multiple times, connect with the database again, and all sort of things like that and I'm sure there is a better and prettier way to do this.

问题是由ajax加载的那些页面也是使用控制器,模型和视图生成的,对于像我这样的人来说事情变得有点复杂(='没有经验')。为了动态获取数据我有时需要包含一个模型两次,包括include中的include,多次发送信息,再次连接数据库,以及所有类似的东西,我确信有这是一个更好,更漂亮的方法。

I'm searching for the best approach and common methods for this. I have no experience working with a big project like this. This is a personal project so I have full control and every answer is welcome.

我正在寻找最佳方法和常用方法。我没有与这样的大项目合作的经验。这是一个个人项目,所以我完全控制,欢迎每一个答案。

Thanks!!!

谢谢!!!

2 个解决方案

#1


0  

You can check the X-Requested-With header that most js frameworks send to see if the request is coming via ajax. Then you can only output certain data and not the "entire page".

您可以检查大多数js框架发送的X-Requested-With标头,以查看请求是否来自ajax。那么你只能输出某些数据,而不能输出“整页”。

Not sure why you need multiple includes like you say, maybe you need to rework your logic.

不知道为什么你需要像你说的多个包含,也许你需要重新修改你的逻辑。

Post some code and we can be of better help.

发布一些代码,我们可以提供更好的帮助。

#2


0  

"To dynamically get the data I some times need to include a model twice, include an include in an include in an include, send information multiple times, connect with the database again, and all sort of things like that and I'm sure there is a better and prettier way to do this."

“为了动态获取数据我有时需要包含一个模型两次,包括include中的include,多次发送信息,再次连接数据库,以及所有类似的东西,我肯定在那里这是一种更好,更漂亮的方式。“

I think you need a better 'design' for you MVC application. Multiple includes - i am guessing in the different layers of the MVC framework might be indicators that your design needs more attention.

我认为你需要为你的MVC应用程序提供更好的'设计'。多个包含 - 我猜测MVC框架的不同层可能是您的设计需要更多关注的指标。

I'll try and be short:

我会尝试做空:

Other frameworks i.e. handle requests via XMLHTTPRequest by AMONG others, disabling or enabling the VIEW or LAYOUT explicitly - check Zend Framework - (e.g. you need to send a JSON encoded string as a response). These requests are handled just as any other request.

其他框架,即由AMONG其他人通过XMLHTTPRequest处理请求,显式禁用或启用VIEW或LAYOUT - 检查Zend Framework - (例如,您需要发送JSON编码的字符串作为响应)。这些请求的处理方式与其他任何请求一样。

I would suggest you have a look at other popular frameworks. Check the application design and layout, and pay attention on the Routing and Dispatching of Actions. I suggest you follow this path since as you say you lack experience.

我建议你看看其他流行的框架。检查应用程序的设计和布局,并注意Actions的Routing和Dispatching。我建议你按照这条路走,因为你说你缺乏经验。

good luck with your project.

祝你的项目好运。

#1


0  

You can check the X-Requested-With header that most js frameworks send to see if the request is coming via ajax. Then you can only output certain data and not the "entire page".

您可以检查大多数js框架发送的X-Requested-With标头,以查看请求是否来自ajax。那么你只能输出某些数据,而不能输出“整页”。

Not sure why you need multiple includes like you say, maybe you need to rework your logic.

不知道为什么你需要像你说的多个包含,也许你需要重新修改你的逻辑。

Post some code and we can be of better help.

发布一些代码,我们可以提供更好的帮助。

#2


0  

"To dynamically get the data I some times need to include a model twice, include an include in an include in an include, send information multiple times, connect with the database again, and all sort of things like that and I'm sure there is a better and prettier way to do this."

“为了动态获取数据我有时需要包含一个模型两次,包括include中的include,多次发送信息,再次连接数据库,以及所有类似的东西,我肯定在那里这是一种更好,更漂亮的方式。“

I think you need a better 'design' for you MVC application. Multiple includes - i am guessing in the different layers of the MVC framework might be indicators that your design needs more attention.

我认为你需要为你的MVC应用程序提供更好的'设计'。多个包含 - 我猜测MVC框架的不同层可能是您的设计需要更多关注的指标。

I'll try and be short:

我会尝试做空:

Other frameworks i.e. handle requests via XMLHTTPRequest by AMONG others, disabling or enabling the VIEW or LAYOUT explicitly - check Zend Framework - (e.g. you need to send a JSON encoded string as a response). These requests are handled just as any other request.

其他框架,即由AMONG其他人通过XMLHTTPRequest处理请求,显式禁用或启用VIEW或LAYOUT - 检查Zend Framework - (例如,您需要发送JSON编码的字符串作为响应)。这些请求的处理方式与其他任何请求一样。

I would suggest you have a look at other popular frameworks. Check the application design and layout, and pay attention on the Routing and Dispatching of Actions. I suggest you follow this path since as you say you lack experience.

我建议你看看其他流行的框架。检查应用程序的设计和布局,并注意Actions的Routing和Dispatching。我建议你按照这条路走,因为你说你缺乏经验。

good luck with your project.

祝你的项目好运。