我应该在哪里保持Angularjs文件在我的web应用程序,我应该怎么构建我的路线和资源? [英] Where should I keep the Angularjs files in my web application and how should I structure my routes and resources?

查看:139
本文介绍了我应该在哪里保持Angularjs文件在我的web应用程序,我应该怎么构建我的路线和资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用Laravel和RESTful Web服务与Angluarjs前端的单页的应用程序。现在,我应该在哪里放置Angularjs文件?我是否应该将它们放置在Laravel安装的公共文件夹或者我应该让他们完全独立的,因为我会在Web服务中发布的资源调用,它会返回JSON数据,因此没有必要对他们在相同的地方。标准或最佳实践是什么?

I am building a RESTful web service using Laravel and a single page application on the front end with Angluarjs. Now where should I place the Angularjs files? Should I place them in the public folder of the Laravel installation or should I keep them completely separate since the I would be issuing calls to the resources in the web service and it would return JSON data and hence there is no need for them to be in the same place. What is the standard or the best practice?

现在,为对第二部分。我应如何管理我的路线和资源Laravel如果我建立一个简单的Todo应用程序。这是我得到真正困惑,这是一个有点难以解释,但留下来陪我一分钟。比如我有一个用户在/用户资源,我可以通过发出AT /用户GET请求获取所有用户或在/用户发出POST请求创建一个新用户。同样地,我可以发出在/用户/ 1的GET请求,并获取第一用户等其他要求动词。现在,我有另一种资源被称为任务。我应该如何实现这个资源?我应该实现它像嵌套资源说/用​​户/ {USER_ID} /任务。当我发出在/用户/ 1 /任务的GET请求,它将获取第一个用户的所有任务。但现在它变得复杂,因为如果问题在一个GET请求/用户/ 10 /任务/ 1,应该把它取第十届用户的第一个任务。但这种逻辑的实现变得我得找10号用户的第一个任务非常困难。

Now, for the second part. How should I manage my routes and resources in Laravel if I am building a simple Todo application. This is where I get really confused and it is a little hard to explain but stay with me for a minute. For example I have a users resource at /users and I can fetch all users by issuing a GET request at /users or create a new users by issuing a POST request at /users. Similarly I can issue a GET request at /users/1 and fetch the first user and so on with other request verbs. Now, I have another resource called tasks. How should I implement this resource? Should I implement it like nested resource say /users/{user_id}/tasks. When I issue a GET request at /users/1/tasks, it will fetch all the tasks for the first user. But now it gets complicated because if issue a GET request at /users/10/tasks/1, should it fetch the first task for the 10th user. But then the implementation of such a logic becomes very difficult as I have to look for the first task of the 10th user.

我只在/用户/ {USER_ID} /任务,这显然会返回指定用户的所有任务设置一个GET路线想出解决的办法。然后,我将创建一个完全不同的资源/任务,以处理所有其他请求动词。我这样做对吗?

I figured a way around this by only setting a GET route at /users/{user_id}/tasks which will obviously return all the tasks for the specified user. And then I would create a completely different resource /tasks to handle all the other request verbs. Am I doing this right?

和还什么,如果我使用的NoSQL数据库一样MongoDB的,在这种情况下,数据将被存储在这样的方式/用户/ {USER_ID} /任务/ {TASK_ID}不会很难,因为任务实现每个用户都在自己的JSON对象。

And also what if I am using a NoSQL DB like MongoDB, in which case the data will be stored in such a manner that /users/{user_id}/tasks/{task_id} will not be difficult to implement since the tasks of every user will be in their own JSON object.

我在正确的方向在想什么?我是新手,我不知道到底是什么处理这一数据库体系结构问题的标准方法是什么?什么是最好的做法是什么?

Am I thinking in the right direction? I am newbie and I don't exactly know what is the standard way to approach such database architecture problems? What are the best practices?

推荐答案

下面是我的建议。

基本上,您需要将您的应用程序划分成模块。请说例如登录,功能1,功能2,以此类推。现在你应该有文件夹的每个模块,你可以将所有与之相关的(controllers.js,services.js,index.html的,style.css文件,filters.js和[名称的文件模块] .js文件)你分开所有code,这使得移动你的code可行这种方式。

Basically you need to divide your application into modules. Say for e.g. login, feature 1, feature 2 etc. Now you should have folder for each module where you can keep all the files related to it (controllers.js, services.js, index.html, style.css, filters.js and [name of module].js) THis way you separate all your code which makes it feasible to move your code.

您可以定义模块为:

(function () {
    'use strict';
    angular
        .module('myapp.login', [
            'myapp.login.controllers',
            'myapp.login.services'
        ]);
}());

和你需要包括这在app.js,像这样

And you need to include this in the app.js, like this

angular
    .module('myapp', [ 'ngRoute',
    'ngResource',
    'myapp.login'
])

此方式,您可以添加所有的模块。

This way you can add all your modules.

一个你想有更重要的文件夹是普通文件夹。在这里你可以包含所有你可能在你的应用程序中的可重用组件模块。

One more important folder that you would like to include is the common folder. Where you can include modules for all the reusable components that you might have in your application.

有关测试您可以与里面(E2E和单位文件夹)的单独测试文件夹。我一直在使用的测试黄瓜,我已经创建,我创造的,我定义功能的每个模块的文件夹功能文件夹中。你还可以创建一个文件夹名为步骤,你可以有单独的js文件为每个模块。

For testing you can have a separate test folder with (e2e and unit folders) inside it. I have been using cucumber for testing and I have created features folder where I create folders for each module in which I define features. Also you can create a folder named steps where you can have separate js files for each module.

在它你可以有配置文件,您可以在您所有的API创建变量的,这样就可以从一个地方控制它的上面。

On top of it you can have config file where you can create variables for all your api's so that you can control it from one place.

希望这有助于:)

这篇关于我应该在哪里保持Angularjs文件在我的web应用程序,我应该怎么构建我的路线和资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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