角和Laravel [英] Angular and Laravel

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

问题描述

如何设置的XAMPP Laravel和Angular.js文件夹结构。我明白创建一个REST风格Laravel API。

How to setup Laravel and Angular.js folder structure on XAMPP. I understand creating an RESTFULL Laravel API.

我一直在一个应用程序,但是当我在地址在浏览器像这种类型的人脉/约翰,我得到一个Laravel查看。

I have been working on an app, but when I type in the address in the browser something like this "contacts/john" I get a Laravel View.

当我去/,然后点击像约翰一些接触,我得到正确的表和浏览器地址栏中搡人脉/约翰,我刷新页面,并再次获得Laravel查看。

When I go to "/" then click on some contacts like John, I get correct table and browser address bar shoving "contacts/john", I refresh page and again get Laravel View.

推荐答案

据我所知,角请求从通过AJAX服务器的路由,然后通过HTML5历史/ pushState的方法设置浏览器的地址栏到新的位置?

I understand that Angular requests the route from the server via AJAX and then sets the browser's URL bar to the new location via the HTML5 history/pushstate methods?

如果是这样,当你通过重新加载页面请求的URL,它就会通过Laravel路由。然后,Laravel只是供应你已经通过你的路由(有时平原JSON)配置的视图。这是你想要的东西,但只有角的AJAX调用。

If so, when you request the URL via a page reload, it gets routed by Laravel. Then, Laravel just serves the view that you have configured via your routes (sometimes plain JSON). Which is what you want, but only for Angular's AJAX calls.

我建议你添加一个 isAJAX 过滤到您的路线。在你的 filter.php 宣布该过滤器:

I propose you add an isAJAX filter to your routes. In your filter.php declare this filter:

Route::filter('isAJAX', function()
{
    if (!Request::AJAX()) return Redirect::to('/')->with(array('route' => Request::path()));
});

然后把所有的路线,你只需要通过AJAX访问成了一团。在你的 routes.php文件

Route::group(array('before' => 'isAJAX'), function()
{
    Route::get('contacts/{name}', ContactController@index); // Or however you declared your route

    ... // More routes
});

这个办法,只有角的AJAX调用返回数据库中的结果。每次手动重新加载页面与一个变量路线设置为路由请求路由回'/'。然后,你将不得不拿起这条路线,并用它启动角的路由器。

This way, only Angular's AJAX calls return results from the database. Every manual page reload is routed back to '/' with a variable route set to the requested route. You would then have to pick up this route and initiate Angular's router with it.

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

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