简体   繁体   English

具有Angular和Laravel API的简单SPA

[英]Simple SPA with Angular and Laravel API

I want to create a really simple SPA with Angularjs and Laravel 4 as an API in the background. 我想用Angularjs和Laravel 4作为后台API创建一个非常简单的SPA。 I'll have some simple posts and categories (a blog actually) with some static pages. 我将提供一些简单的帖子和类别(实际上是一个博客),并带有一些静态页面。 I want to call laravel API inside angular to get the information I want. 我想在angular中调用laravel API以获取所需的信息。 How can I organize the code to do it properly? 如何组织代码以正确执行? Maybe put the laravel to a subdomain and the angular app to the main domain? 也许把laravel放到一个子域,而将角度应用放到主域? Is it a good way? 这是个好方法吗? I want to separate these frameworks and use laravel only for api service. 我想分离这些框架,仅将laravel用于api服务。 Thanks for your advices. 感谢您的建议。

You can use laravel's built in restful controllers as a way of building your backend api. 您可以使用laravel的内置Restful控制器作为构建后端api的一种方式。

Then you just have your angular js make ajax calls to whatever route you define to that controller. 然后,您只需要让您的angular js对您定义到该控制器的任何路由进行ajax调用即可。 Then instead of having laravel return views using View::make, you have it return a response and I'm assuming returning json would be the best option. 然后,不用让laravel使用View :: make返回视图,而是让它返回响应,并且我假设返回json是最佳选择。 return Response::json(array('userId' => '1', 'operation' => 'UpdateUser', 'success' => 'true'));

Implemented this way, laravel acts strictly as an API and angular js acts as the intermediary between your user and the laravel api. 通过这种方式实现,laravel严格充当API,而angular js充当用户和laravel api之间的中介。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM