5

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.

4
  • Can you share more of your Angular code? how is your route configured and if you have things like target="_self" in your link? The PHP side might not be the problem Commented Jun 6, 2015 at 22:28
  • I've included it now Commented Jun 6, 2015 at 22:30
  • Please explain issues in more detail. Not clear what you mean by doesn't reload Commented Jun 6, 2015 at 23:01
  • Hey, did you find a solution to this problem ? Commented Aug 20, 2015 at 8:17

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

using Route::get('{all}') is a very dangerous practice, it can lead to subtle bugs like requesting a javascript file and getting your index view instead. you should consider using App::missing instead, since this will only fire on routes that don't already have a match to a defined route or resource.
I read around that App::missing isn't available in laravel 5
I think in this approach you can't resolve 404 error pages. this answer worked for me with a bit change: stackoverflow.com/questions/16569841/…
This isn't a great fix. If I had a AngularJS route like 127.0.0.1/this/is/my/blog then it would 404...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.