Backbone.js的Pushstate路由和PHP框架的路由器 [英] Backbone.js Pushstate Routing and PHP Framework's Router

查看:142
本文介绍了Backbone.js的Pushstate路由和PHP框架的路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的还有一个PHP MVC框架(codeigniter,Laravel)Backbone.js的。我很困惑如何URL将被PHP路由器和骨干路由器使用Pushstates时进行处理。 (我还没有开始使用pushstates然而,依然采用hashbangs )。我不工作在单页应用程序,而是该网站是由若干个页面像自身的单页应用

I am using Backbone.js along with a PHP MVC framework (Codeigniter, Laravel). I'm confused about how URLs will be handled by the PHP router and Backbone router when using Pushstates. (I have not started using pushstates yet, still using hashbangs #). I am not working on a single-page-application, but rather the site is made up of several pages that act like single-page-application on its own.

问题::如果我有一个页面 http://domain.com/user/user123 ,并单击链接 http://domain.com/user/user123#photos 加载通过AJAX一些照片,我想摆脱的,我预见在哪里谁去 http://domain.com/user/user123/photos 点击链接后,用户决定复制并粘贴URL到另一个问题浏览器选项卡中会看到一个错误404页的PHP框架路由器的(如果我认为正确的PHP路由器之前骨干的第一个处理URL请求)的不承认这个网址的存在。应如何解决?

Problem: If I have a page http://domain.com/user/user123, and clicking a link http://domain.com/user/user123#photos loads some photos via AJAX and I want to get rid of the #, I forsee the problem where a user who goes to http://domain.com/user/user123/photos after clicking on the link decides to copy and paste the URL into another browser tab will see an error 404 page as the PHP framework router (if I assume correctly that the PHP router handles the URL request first before backbone's) does not recognize the existence of this url. How should this problem be solved?

还有一个问题,我认为会出现,是如何当用户直接进入 http://domain.com/user/user123/photos 视图渲染?应在HTML code被复制到两个PHP的视图(为其中用户直接输入URL的实例)和Backbone.js的视图(为的情况下,其中用户点击该链接时从 http://domain.com/user/user123 )?这似乎并不像我的最佳解决方案。或者,可以查看以某种方式在这两种情况下,通过骨干渲染,以避免重复code?

Another problem I think will arise, is how should the view be rendered when the user goes to http://domain.com/user/user123/photos directly? Should the HTML code be duplicated on both the PHP View (for the instance where the user enters the URL directly) and the backbone.js View (for the case where the user clicks on the link from http://domain.com/user/user123)? This does not seem like the optimal solution to me. Or can the View somehow be rendered in both cases by backbone to avoid duplicate code?

请指教:)

推荐答案

您已经打对纳入 pushState 为骨干的应用程序的更具挑战性的方面之一。基本上,你需要确保你会被链接到您的应用程序内的任何路由也由服务器来处理。

You've hit on one of the more challenging aspects of incorporating pushState into a backbone app. Basically, you need to make sure that any route you'll be linking to within your app can also be handled by the server.

要解决这个问题最简单的方法,在PHP中,是途径有效的URL( /用户/ user123 )到你的根页的所有请求,然后让骨干通话对应的URL片段的处理程序。这需要你在服务器端真正知道什么是有效的URL(即 /职位/ 5 是有效的, /职位/ ABC / AAA / 不,等等),但除此之外,它不是一个大量的工作。

The easiest way to solve this, in PHP, is to route all requests for valid URLs (/user/user123) to your root page, and then let backbone call the handler corresponding to the url fragment. This requires you on the server side to actually know what a valid URL is (i.e. /posts/5 is valid, /posts/abc or /aaa/ is not, etc.), but otherwise it's not a huge amount of work.

最后的问题是,你是一个无状态协议(HTTP)和一个国家的FUL应用程序(Backbone.js的)之间的切换。这需要仔细的规划。

Ultimately the problem is that you're switching between a stateless protocol (HTTP) and a state-ful application (backbone.js). This requires some careful planning.

关于你提到的第二个问题,你并不需要渲染在服务器端的观点,只是做的一切,你会做一个请求到根域和Backbone.js的将触发相应的处理程序(如 /用户/ user123 )。在这里,你会想实际呈现在服务器端视图的唯一情况是,如果你想支持没有JavaScript的用户,或者让你的应用程序REST风格,你会在这种情况下,要复制视图code(没有解决这个简单的方法)。

Regarding your second question, you don't need to render the view on the server-side, just do everything you would do for a request to the root domain and backbone.js will trigger the appropriate handler (e.g. /user/user123). The only case where you'd want to actually render the view on the server side is if you want to support users without javascript, or make your app RESTful, in which case you would have to duplicate view code (there is no easy way around this).

希望是有道理的。

这篇关于Backbone.js的Pushstate路由和PHP框架的路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆