Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干路由不适用于Laravel_Javascript_Php_Backbone.js_Laravel - Fatal编程技术网

Javascript 主干路由不适用于Laravel

Javascript 主干路由不适用于Laravel,javascript,php,backbone.js,laravel,Javascript,Php,Backbone.js,Laravel,我不知道错误在哪里 我正在做一个单页应用程序,这是上下文: 我在Laravel有一个资源控制器,它监视这个路由域。dev/v1/ Laravel提供了第一个页面/view/public/views/layouts/application.blade.php Mustache视图存储在public/views/下,当主干路由器调用它们时,它们会同步加载。我已经修改了app/config/view.php文件,以提供来自bakcbone的视图 在主干网中,路由器控制每个URI更改,甚至包括pushs

我不知道错误在哪里

我正在做一个单页应用程序,这是上下文:

我在Laravel有一个资源控制器,它监视这个路由域。dev/v1/ Laravel提供了第一个页面/view/public/views/layouts/application.blade.php Mustache视图存储在public/views/下,当主干路由器调用它们时,它们会同步加载。我已经修改了app/config/view.php文件,以提供来自bakcbone的视图 在主干网中,路由器控制每个URI更改,甚至包括pushstate和相应的Mustache视图。一切似乎都很好,但如果您为用户o列表或用户键入直接URI…您只会看到服务器返回的JSON,而不是相应的主干视图,换句话说,我不知道哪个工作不正确,是Laravel路由器还是主干路由器。或者它是一个Laravel配置? 这是我目前的代码:

// app/routes.php

Route::group(['prefix' => 'v1'],function (){

    Route::resource('users','V1\UsersController');

    Route::get('/', function()
    {
      return View::make('layouts.application')->nest('content', 'app');
    });
});


// app/controllers/V1/UsersController.php

namespace V1;

//import classes that are not in this new namespace
use BaseController;
use User;
use View;
use Input;
use Request;

class UsersController extends \BaseController {

    public function index()
    {
        return $users = User::all(['id','email','name']) ;
    }

    public function show($id)
    {
        $user = User::find($id,['id','email','name']);

        if(is_null($user)){
            return array(
                'id' => 0,
                'email' => 'fake email'
                );
        }
        return $user;
    }

// public/js/namespaces.js

(function(){

    window.App = {
        Models : {},
        Collections : {},
        Views : {},
        Router : {}
    };

    window.Templator = function (mustacheView){
        return $.ajax({
            url: '/views/'+mustacheView,
            async : false,
            type: 'GET',
        }).responseText;
    };

    window.Vent = _.extend({},Backbone.Events); 

})();

// public/js/backbone/router.js

App.Router = Backbone.Router.extend({
    routes : {
        '' : 'home',
        'users' : 'showAll',
        'users/:id' : 'showUser',
        'login' : 'showLoginForm'
    },
    home: function (){
        Vent.trigger('home:index');  
    },
    showAll : function (){
        Vent.trigger('users:showAll');  
    },
    showUser: function (id){
        Vent.trigger('users:show',id);  
    },
    showLoginForm : function (){
        Vent.trigger('login:form');  
    }
});


// public/js/app.js
$(function() {

    $(document).on("click", "a", function(e){
        var href = $(this).attr("href");

      if (href != '#')  {
        e.preventDefault();
        Backbone.history.navigate(href,{trigger:true});
      }
    });

    new App.Views.AllUsers;
    new App.Views.Index;
    new App.Views.Login;
    new App.Router;

    Backbone.history.start({ 
        pushState: true, 
        silent: true,
        root: '/v1' 
    });
});
因此,如果我在导航栏上键入这个URI domain.dev/v1/users,就会显示JSON中的用户列表,而不会显示主干中关联的视图。
有什么建议吗?

看看我刚才在关于角度的类似问题中给出的答案:

在课文中,用主干代替棱角

如何通过从Laravel到主干的路线:

在包含backbone.js之前,从Laravel为“/”路由返回的基本视图需要在其内部某处具有类似的内容:

<script>
    var myRoute = "{{ $route }}";
</script>

这应该可以解决问题。

我对主干并不特别熟悉,但就Laravel代码而言,您得到了您应该得到的。控制器操作返回一个用户数组,Laravel会自动将其转换为JSON响应。如果你想从Laravel返回一个视图,你应该返回view::make'view_name',但是我猜你在主干应用程序中有一些视图的数据绑定,所以如果直接路由到/v1/用户返回一个视图而不是数组,你需要处理这个问题。是的,你是对的@Bogdan。Laravel restful API只返回和接收JSON。但我认为问题可能在于主干的深度连接。我现在真的很困惑,我不知道还有什么地方可以检查我想你的解决方案会奏效@sisou但是。。。如何处理传递到服务器呈现的主页的变量路由??在您的示例->return Redirect:to'/'中,我还编辑了我的答案,给出了从Laravel到主干的路由变量的获取方向。它似乎很完美,我将尝试对结果进行注释;完美的我只需要对我的拉威尔过滤器和路线做一些调整,瞧!谢谢你@Sisou很高兴我能帮忙。快乐编码!:
Backbone.history.start(); // You should already have this line, so just add the next one
App.nav.navigate(myRoute);