简体   繁体   中英

Laravel 5 + AngularJS html5Mode

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM