html5Mode 中 Laravel 4 和 AngularJS 的路由问题
Routing issue with Laravel 4 and AngularJS in html5Mode
我正在创建一个 RESTful 应用程序,其中 Laravel 4 作为后端,AngularJS 作为前端。我想使用来自 Angular.
的路由
routes.php
在laravel中是这样设置的(有一组/api,但不是必须的)
Route::any('{all}', function ($uri) {
return View::make('index');
})->where('all', '.*');
我在 Angular 的路线是
$routeProvider.
when('/', {
templateUrl: 'views/test.html',
controller: 'homeCtrl'
}).
when('/test', {
templateUrl: 'views/test.html',
controller: 'homeCtrl'
}).
when('/test/:id', {
templateUrl: 'views/test.html',
controller: 'homeCtrl'
}).
otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
我在index.php
中定义了<base href="/"/>
我在 xampp 中的 DocumentRoot 在 DocumentRoot "C:/xampp/htdocs/laravel/public"
中(所以 public 文件夹在 Laravel 中)
当我点击 localhost/test
时一切正常。但是当我尝试访问 localhost/test/1
时,所有内容都从 Laravel 视图加载为 index.php
文件,如下所示。
有人解决这个问题吗?我是否应该在 routes.php
中创建正则表达式,它将包含每个(.js、.jpg、...)扩展(我认为这不是个好主意)?或者我应该更改 .htaccess
文件?
我在这个 question 中找到了一个解决方案,并将 Laravel 中丢失的捕获路由更改为这个,一切都很好。
App::missing(function ($exception) {
return Redirect::to('/#/' . Request::path());
});
问题是
- Angular 在 html5Mode 中需要
#
作为前缀。
- Laravel 正在返回子路径中的主页,包括我在问题中显示的资产。
我正在创建一个 RESTful 应用程序,其中 Laravel 4 作为后端,AngularJS 作为前端。我想使用来自 Angular.
的路由routes.php
在laravel中是这样设置的(有一组/api,但不是必须的)
Route::any('{all}', function ($uri) {
return View::make('index');
})->where('all', '.*');
我在 Angular 的路线是
$routeProvider.
when('/', {
templateUrl: 'views/test.html',
controller: 'homeCtrl'
}).
when('/test', {
templateUrl: 'views/test.html',
controller: 'homeCtrl'
}).
when('/test/:id', {
templateUrl: 'views/test.html',
controller: 'homeCtrl'
}).
otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
我在index.php
<base href="/"/>
我在 xampp 中的 DocumentRoot 在 DocumentRoot "C:/xampp/htdocs/laravel/public"
中(所以 public 文件夹在 Laravel 中)
当我点击 localhost/test
时一切正常。但是当我尝试访问 localhost/test/1
时,所有内容都从 Laravel 视图加载为 index.php
文件,如下所示。
有人解决这个问题吗?我是否应该在 routes.php
中创建正则表达式,它将包含每个(.js、.jpg、...)扩展(我认为这不是个好主意)?或者我应该更改 .htaccess
文件?
我在这个 question 中找到了一个解决方案,并将 Laravel 中丢失的捕获路由更改为这个,一切都很好。
App::missing(function ($exception) {
return Redirect::to('/#/' . Request::path());
});
问题是
- Angular 在 html5Mode 中需要
#
作为前缀。 - Laravel 正在返回子路径中的主页,包括我在问题中显示的资产。