客户端Javascript应用程序 - 没有散列标记的URL路由 [英] Client-side Javascript app - url routing with no hash tag

查看:148
本文介绍了客户端Javascript应用程序 - 没有散列标记的URL路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新版本的Ember.js,使用新的仅限客户端的应用程序。有一个PHP页面构建脚本,css,模板文件等,并将其全部传输到index.php中。我正在使用一个htaccess指令,以便将所有请求重写为/index.php。 PHP只是为了方便地打包Javascript,就我而言。

I'm working on a new client-side only app with the latest version of Ember.js. There is a single PHP page which builds the scripts, css, template files, etc. and delivers it all into index.php. I'm using an htaccess directive so that all requests are rewritten to /index.php. The PHP is only there to conveniently package the Javascript, as far as I'm concerned.

目前,浏览器中的路由器看起来像这样,工作正常。 p>

Currently, routes in the browser look like this and work just fine.

/#/about 
/#/favorites
/#/etc
/#/posts/5/edit

但是,我希望他们看起来像这样 - 哪些不工作。

However, I would like them to look like this - which do not work just fine.

/about
/favorites
/etc
/posts/5/edit

完全相同的客户端代码仍然随第二个选项一起提供 - 但它始终会打到索引路由处理程序。我已经看到客户端应用程序拉开这个 - 我什么失踪? PHP需要匹配的路由处理程序吗?

The exact same client-code is still delivered with the second option - but it always hits the index route handler. I've seen client-side apps pull this off before - what am I missing? Do I need to have matching route handlers on the PHP side?

编辑:我正在寻找一个具体的解决方案。网络充满了哦 - 你只是做这个信息,让别人刮目相看。

I'm looking for a specific answer of how to approach this. The web is full of "oh - you just do this" information that leaves everybody else scratching their heads.

推荐答案

在Ember中。 js(版本1.0.0rc3)可以通过使用 Ember.js位置API

In Ember.js (version 1.0.0rc3) this can be accomplished by using the Ember.js location API:

App.Router.reopen({
  location: 'history'
});

然后设置Web服务器将流量重定向到Ember应用程序。

And then setting the web server to redirect traffic to the Ember application.

给出一个具体的例子是一个基本的Apache .htaccess 文件将流量重定向到位于index.html中的Ember应用程序:

To give a concrete example here is a basic Apache .htaccess file redirecting traffic to the Ember application located in index.html:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html#$1 [L]

As Alex White 指出,Apache 2.2.16及更高版本支持将流量重定向到单个目标的更简单的配置:

As Alex White points out Apache 2.2.16 and above supports a simpler configuration for redirecting traffic to a single target:

FallbackResource /index.html

FallbackResource mod_dir 模块,需要设置 AllowOverride Indexes

请确保彻底测试应用路由。一个常见的错误是未捕获的SyntaxError:意外的标记< ,这是通过使用CSS和JS文件的相对链接引起的。使用 / 标记来预备它们,使它们成为绝对的。

Make sure to test the application routes thoroughly. One common error is Uncaught SyntaxError: Unexpected token <, which is caused by using relative links to CSS and JS files. Prepend them with a / mark to make them absolute.

此功能是 Internet Explorer< 10 中不支持。

This functionality is not supported in Internet Explorer <10.

这篇关于客户端Javascript应用程序 - 没有散列标记的URL路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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