我是 Backbone 和 Laravel 的新手,所以这可能是一个简单的问题,但我需要我的 Backbone 路由器来匹配初始页面加载时的 URL,例如 localhost/search/query,我希望在没有碎片标识符的情况下执行此操作(或 # hashtags 或其他)。这给了我两个问题:
当我使用时,
Backbone.history.start({ pushState: true })
我从 Laravel 收到 404 错误。有没有办法为 Laravel 中的所有 URL 定义默认(索引)路由,这样我就不必列出我可能使用的所有可能的 URL 组合?当我不使用
pushState: true
我的 Backbone 路由器时工作正常,但前提是我不直接导航到 URL。例如,如果我首先访问 localhost,则 localhost/#search/example 它可以工作,但如果我直接访问 localhost/#search/example,则不会触发搜索路由。我认为这可能是因为我只在准备好文档时才启动 Backbone.history,但我不知道该怎么做。无论如何,这里有一些代码://app.js window.App = { Models: {}, Collections: {}, Views: {}, Router: {} }; window.vent = _.extend({}, Backbone.Events); // router.js App.Router = Backbone.Router.extend({ routes: { '': 'index', 'search/:query': 'search' }, index: function() { console.log('the index page'); }, search: function(query) { console.log('the search page: ' + query); vent.trigger('router:search', query); } }); // setup.js (function() { App.cats = new App.Cats(); App.appView = new App.AppView(); App.router = new App.Router; Backbone.history.start({ pushState: true }); })();
提前感谢您的任何建议!