我希望使用Angularjs的
例如:地址栏显示
一切都运作良好,直到我刷新页面。如果我刷新,将访问以下在routes.php中定义的Laravel路由。
$locationProvider.html5Mode(true)
来从URL中移除#哈希。例如:地址栏显示
http://localhost/shop
而不是http://localhost/#/shop
。一切都运作良好,直到我刷新页面。如果我刷新,将访问以下在routes.php中定义的Laravel路由。
Route::resource('shop', 'ShoppingController')
不是AngularJS Route(在app.js中定义)
$routeProvider.when('/shop', {
templateUrl: 'templates/shop.html',
controller: 'ShoppingController'
});
我的代码:
routes.php (Laravel 路由)
Route::get('/', function() {
return View::make('index');
});
Route::resource('shop', 'ShoppingController');
app.js(AngularJS路由)
var app = angular.module('shoppingApp',['ngRoute','SharedServices']);
app.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/shop', {
templateUrl: 'templates/shop.html',
controller: 'ShoppingController'
});
$routeProvider.otherwise({ redirectTo: '/' });
$locationProvider.html5Mode(true);
});
我的目录结构:
Project
/app
/...
/views
-index.php (single page application file)
-routes.php (Laravel routes)
/public
/...
/js
-angular.js
-app.js
-index.php (Laravel index file)
已尝试的解决方案:
重写htaccess文件,使所有请求都重定向到index.php(单页应用程序文件,从这里AngularJS将接管路由)。 问题:通过这种方式,Laravel路由(Route :: resource('shop','ShoppingController'); - 与数据库交互所必需的)对于AngularJS $http服务变得不可访问:
app.js
app.controller("ShoppingController", function($scope, $http) {
$http.get('/shop', { cache: true}).
success(function(data, status) {
$scope.items = data
}).
error(function(data, status) {
console.log('Status: ' + status);
});
});
问题: 如何解决路由问题,使得在刷新 localhost/shop 时访问的是 AngularJS 路由而非 Laravel 路由?