0

I try to create an app with Laravel 5.3 and AngularJS. I want to use the routes and templates from Angular instead of Laravel.

Here is the web.php file from Laravel:

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

And here is a part of the ui-router in AngularJS:

routeConfig.$inject = ['$stateProvider'];
function routeConfig ($stateProvider) {
  // Routes
  $stateProvider
    .state('home', {
      url: '/',
      templateUrl: 'app/views/home.html'
    })
    .state('register', {
      url: '/register?oauth_token&oauth_verifier',
      templateUrl: 'app/views/register.html',
      controller: 'RegisterController',
      controllerAs: 'registerCtrl'
    })
  };

I have also enabled the html5mode and the base url on head. The problem now:

When I am at home and click the link to go on register page, it works. But If I try to load directly the register page, it loads it through laravel routes and since I haven't mentioned anything about it there, I have a NotFoundHttpException.

Tasos
  • 7,325
  • 18
  • 83
  • 176

1 Answers1

1

That's because when you refresh all routes are handled by laravel first.

I had the same problem and there are 2 approaches:

either put the angular app on a different domain ... but you will run into CORS issues

or tell laravel to route any route to angular app, and that's easier

Route::any('{path?}', function()
{
    return view("index");
})->where("path", ".+");
Sherif
  • 1,477
  • 1
  • 14
  • 21