2

I am creating a RESTful application with Laravel 4 as backend and AngularJS as frontend. I want to use routing from Angular.

routes.php in laravel are set up like this (with one group /api, but it is not necessary)

Route::any('{all}', function ($uri) {
    return View::make('index');
})->where('all', '.*');

My routes in Angular are

$routeProvider.
    when('/', {
        templateUrl: 'views/test.html',
        controller: 'homeCtrl'
    }).
    when('/test', {
        templateUrl: 'views/test.html',
        controller: 'homeCtrl'
    }).
    when('/test/:id', {
        templateUrl: 'views/test.html',
        controller: 'homeCtrl'
    }).
    otherwise({
        redirectTo: '/'
    });
 $locationProvider.html5Mode(true);

I defined <base href="/"/> in index.php

And my DocumentRoot in xampp is in DocumentRoot "C:/xampp/htdocs/laravel/public" (so public folder in Laravel)

When I hit localhost/test everything works fine. But when I try access to localhost/test/1, everything is loaded as index.php file from Laravel views as you can see bellow.

enter image description here

Does anyone solve this problem? Should I create regex in routes.php, which will hold every (.js, .jpg, ...) extensions (i think it is not good idea)? Or should I change .htaccess file?

Jozef Dúc
  • 965
  • 2
  • 18
  • 29
  • https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode. Laravel isn't specifically listed, but you should be able to get an idea of how to configure it based on the other examples. – Claies Jun 07 '15 at 16:36
  • @Claies, it is not problem in `$locationProvider.html5Mode(true);` (i commented it out with same result), i think. – Jozef Dúc Jun 07 '15 at 16:42
  • also, possible duplicate of http://stackoverflow.com/questions/21357258/angularjs-and-laravel-4-routing-conflict-in-html5-mode?rq=1 – Claies Jun 07 '15 at 16:42
  • true, it's not a problem of `$locationProvider` specifically, it's a problem with the way your server is parsing requests. But the correct configuration for a server is shown in those examples. – Claies Jun 07 '15 at 16:44
  • i.e. don't use `Route::any('{all}'` for angular, use `App::missing` instead. – Claies Jun 07 '15 at 16:46
  • I read every answer from that question before and the correct one was last, which I did not try http://stackoverflow.com/a/30304640/3937482. Thanks! – Jozef Dúc Jun 07 '15 at 16:57

1 Answers1

0

I found a solution in this question and changed missing routes catching in Laravel to this one and everything is fine.

App::missing(function ($exception) {
    return Redirect::to('/#/' . Request::path());
});

The problems were

  • Angular in html5Mode need # as a prefix.
  • Laravel was returning main page in subroutes including assets as I showed in the question.
Community
  • 1
  • 1
Jozef Dúc
  • 965
  • 2
  • 18
  • 29