Laravel 5 + AngularJS html5Mode

2019-02-17 06:41发布

I'm developing a project using Laravel 5 and AngularJS. I want to enable

$locationProvider.html5Mode(true);

and stop the page from reloading. The page doesn't reload when I set it to false and visit a link.

Here is my route.php

Route::get('/', function () { 
    return View::make('index'); 
});

Angular code

app.config(function($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
        templateUrl: 'views/feed.html',
        controller: 'fdController'
    }).when('/collections', {
        templateUrl : 'views/collections.html',
        controller: 'clController'
    }).otherwise({
        redirectTo : '/'
    });

    $locationProvider.html5Mode(true);
});

When I visit a link html5Mode(false) localhost:8000/#/ -> localhost:8000/#/feed the page doesn't refresh

When html5Mode(true) and I visit localhost:8000/ -> localhost:8000/feed, the page refreshes and I get this error:

Sorry, the page you are looking for could not be found.

1条回答
狗以群分
2楼-- · 2019-02-17 07:31

I changed my route.php to

Route::get('/', function () { 
    return View::make('index'); 
});


Route::get('{all}', function () { 
    return View::make('index'); 
});

And added a base <base href="/"> to my index.php Everything works now and the page doesn't refresh.

查看更多
登录 后发表回答