html5Mode 中 Laravel 4 和 AngularJS 的路由问题 [英] Routing issue with Laravel 4 and AngularJS in html5Mode

查看:26
本文介绍了html5Mode 中 Laravel 4 和 AngularJS 的路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个以 Laravel 4 作为后端和 AngularJS 作为前端的 RESTful 应用程序.我想使用来自 Angular 的路由.

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

routes.php是这样设置的(有一组/api,但不是必须的)

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', '.*');

我在 Angular 中的路线是

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);

我在index.php

我在 xampp 中的 DocumentRoot 位于 DocumentRoot "C:/xampp/htdocs/laravel/public"(Laravel 中的公共文件夹)

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

当我点击 localhost/test 时,一切正常.但是当我尝试访问 localhost/test/1 时,所有内容都从 Laravel 视图加载为 index.php 文件,如下所示.

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.

有没有人解决这个问题?我应该在 routes.php 中创建正则表达式,它将保存每个 (.js, .jpg, ...) 扩展名(我认为这不是一个好主意)?或者我应该更改 .htaccess 文件?

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?

推荐答案

我在这个问题 并将 Laravel 中丢失的路由更改为此路由,一切正常.

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());
});

问题是

  • html5Mode 中的 Angular 需要 # 作为前缀.
  • Laravel 在子路由中返回主页,包括我在问题中展示的资产.

这篇关于html5Mode 中 Laravel 4 和 AngularJS 的路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆