Routing issue with Laravel 4 and AngularJS in html5Mode

≡放荡痞女 提交于 2019-12-22 12:27:42

问题


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

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', '.*');

My routes in Angular are

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

I defined <base href="/"/> in index.php

And my DocumentRoot in xampp is in DocumentRoot "C:/xampp/htdocs/laravel/public" (so public folder in 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.

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?


回答1:


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());
});

The problems were

  • Angular in html5Mode need # as a prefix.
  • Laravel was returning main page in subroutes including assets as I showed in the question.


来源:https://stackoverflow.com/questions/30695741/routing-issue-with-laravel-4-and-angularjs-in-html5mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!