路由问题与Laravel 4和html5Mode AngularJS路由、问题、Laravel、html5Mode

2023-09-13 03:32:45 作者:茶凉缘尽

我创造与Laravel 4 RESTful应用程序作为后端和AngularJS作为前端。我想用路由从棱角分明。

I am creating a RESTful application with Laravel 4 as backend and AngularJS as frontend. I want to use routing from Angular.

routes.php文件在laravel都设置这样的(用一组/​​ API,但不是必要的)

routes.php in laravel are set up like this (with one group /api, but it is not necessary)

Route::any('{all}', function ($uri) {
    return View::make('index');
})->where('all', '.*');

我的角度航线

$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);

我定义的<基本href =//> 的index.php

和我的XAMPP的DocumentRoot是的DocumentRootC:/ XAMPP / htdocs中/ laravel /公众(在Laravel所以公用文件夹)

And my DocumentRoot in xampp is in DocumentRoot "C:/xampp/htdocs/laravel/public" (so public folder in Laravel)

当我打本地主机/测试一切工作正常。但是,当我试图访问本地主机/测试/ 1 ,一切都加载的index.php 文件从Laravel意见你可以看到波纹管。

When I hit localhost/test everything works fine. But when I try access to localhost/test/1, everything is loaded as index.php file from Laravel views as you can see bellow.

有谁解决这个问题?我应该创造 routes.php文件正则表达式,这将持有每(.js文件,JPG格式,...)扩展(我认为这不是好主意)?或者我应该改变的.htaccess 文件?

Does anyone solve this problem? Should I create regex in routes.php, which will hold every (.js, .jpg, ...) extensions (i think it is not good idea)? Or should I change .htaccess file?

推荐答案

我发现这个question并改变路线缺失在捕到Laravel这一块,一切都很好。

I found a solution in this question and changed missing routes catching in Laravel to this one and everything is fine.

App::missing(function ($exception) {
    return Redirect::to('/#/' . Request::path());
});

该问题是

角在html5Mode需要为preFIX。 Laravel在子路径包括资产,因为我在这个问题表现出返回主页面。 Angular in html5Mode need # as a prefix. Laravel was returning main page in subroutes including assets as I showed in the question.