1

I'm new to AngularJS and having a hard time getting it to work smoothly with Laravel. I am trying to create a back-end application for a website and want the routes prefixed with '/admin' to all be managed by AngularJS without the hash in the URL.

Laravel's Back-end

Routes

Route::group(['prefix' => 'admin'], function()
{
    Route::get('/', function() {
        return View::make('backend/dashboard');
    });
});

View - dashboard.blade.php

<!doctype html>
<html ng-app="glenn">
<head>
    <title>Laravel and Angular Together</title>
    <meta charset="utf-8">

    <base href="/admin">

    <script src="vendor/angular.js"></script>
    <script src="vendor/angular-route.js"></script>
    <script src="js/app.js"></script>
</head>
<body>
    <h1>Dashboard</h1>
    <div class="container">
        <ng-view></ng-view>
    </div>
</body>
</html>

AngularJS Front-end

app.js

var glenn = angular.module('glenn', ['ngRoute']);

glenn.config(function($routeProvider, $locationProvider) {
    $routeProvider
        .when('/test', {
            templateUrl: 'test.html',
            controller: 'TestController'
        })
        .otherwise({
            redirectTo: '/'
        });

    $locationProvider.html5Mode(true);
});

glenn.controller('TestController', function($scope) {

});

Problem: when I direct the browsers url to admin/test, then I receive Laravel's NotFoundHttpException because Laravel is trying to take over the routes that I want Angular to take care of...how can I fix this? Thanks

2 Answers 2

1

Since angular will be handling all the routing for admin (and you won't be specifying any further routing in Laravel), you can probably do without the prefix.

Try:

Route::get('admin{angular}', function () {
    return View::make('backend/dashboard');
})->where('angular', '.*');

Instead of:

Route::group(['prefix' => 'admin'], function()
{
    Route::get('/', function() {
        return View::make('backend/dashboard');
    });
});

This should catch all routes that begin with admin, which is what I believe you want.

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

Comments

1

On a related note:

If you were creating an Angular + Laravel web app where you wanted all routing to be handled by AngularJS you can specify a 'catch-all' route within Laravel to avoid the NotFoundHttpException error:

App::missing(function($exception)
{
    return View::make('backend/dashboard'); // this should match your index route
});

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.