2

In using AngularJS on top of Laravel, assuming I have this route in Laravel:

Route::get('/admin/', function() { return View::make('pages.writer'); });

which makes a view that loads AngularJS components, including $routeProvider, and that the configuration is like so:

 $routeProvider.when('/writer/:publicationName?', {
   templateUrl: '/templates/writer.html',
   controller: 'WriterCtrl',
   caseInsensitiveMatch: true
 }).otherwise({
   templateUrl: '/templates/library.html',
   controller: 'LibraryCtrl'
 });

If I call the URL: /admin/writer/My-Favorite-Publication, I get a Laravel error because the route doesn't exist. Is there a way to tell Laravel when Angular routing takes over for a particular URL (i.e. anything after /admin/), so that I can use $routeParams.

6
  • have you enabled html5mode? Commented Aug 15, 2015 at 18:39
  • @PankajParkar yes I have Commented Aug 15, 2015 at 18:44
  • I think by putting <base href="/admin"> on your html head tag will fix the issue Commented Aug 15, 2015 at 18:46
  • @PankajParkar I did that too! I should perhaps clarify that the URL is called directly, not via $location.path(). Commented Aug 15, 2015 at 18:47
  • 1
    @PankajParkar none of those will work. Before anything is returned to the browser or angularjs app it first needs to pass through Laravel and the application breaks at this stage before Angular app is executed Commented Aug 15, 2015 at 18:49

1 Answer 1

2

You need to define a catch-all route that will return the base view that runs AngularJS app and let AngularJS handle the rest of the routing.

This will do the trick:

Route::get('/admin/{ignore?}', function() { 
  return View::make('pages.writer'); 
})->where('ignore', '.*');

This will make everything that starts with /admin match this Laravel route.

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

Comments

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.