0

I have an Angular app that uses PHP for the API and a little bit for the frontend as well.

Routing has become impossible; only 1st level routes are returned.

My structure is as follows:

  • /app/index.html
  • /app/scripts/[all angular files]
  • /app/views/[all PHP-parsed HTML files]
  • /app/api/[app PHP files]

Router (Angular):

angular.module('MyApp')
.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {

$locationProvider.html5Mode(true);

$routeProvider
    .when('/', {redirectTo: '/home'})
    .when('/home', {
        templateUrl: 'views/mainview.php?sec=home',
        controller: 'HomeCtrl'
    })
    .when('/user', {
        templateUrl: 'views/mainview.php?sec=user',
        controller: 'ProfileCtrl'
    });

vhosts Apache file

<VirtualHost 127.0.0.2:80>
  DocumentRoot "M:/site/app"
  # names and emails commented out
</VirtualHost>

.htaccess

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  RewriteRule ^/user/([0-9]+)$ /user?user=$1
</IfModule>

When I access 127.0.0.2, it redirects to /home and works fine. When I access /user?id=1, it also works fine. But if I retrieve /user/1, then only the main php-parsed HTML loads, but all dependencies (scripts, css, whatever else) just return the same main HTML file, thus failing.

I have seen many StackOverflow responses and so far I have not been able to fix anything...

Can anyone help me make routing happen?

Community
  • 1
  • 1
Jago
  • 2,751
  • 4
  • 27
  • 41

1 Answers1

0

It doesn't appear that you have a route for /user/1. Try something like

.when('/user/:userId')

Check out the example in angular's documentation: https://docs.angularjs.org/api/ngRoute/service/$route#example

  • Indeed i don't. I just expect Apache to translate my /user/1 to /user?id=1 and Angular will take care of that – Jago Sep 19 '15 at 10:01
  • My mistake, I see what you're asking now. Is there a reason you need to add this at the Apache httpd level? You could setup multiple angular routes to use the same handler. Good luck. – scottseeker Sep 20 '15 at 20:21
  • AFAIK Apache tries to take care of any route that is deeper than 1 level before Angular can handle it. So That /user works, but /user/1 does not. Or at least that is what seems to happen here. That is why I have to tell Apache to translate routes to 1 level depth. Either that or just tell Apache to NOT HANDLE any route in the vhost. – Jago Sep 22 '15 at 09:26