在 Laravel 4 中构建 SAAS 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18290863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Right approach to building SAAS in Laravel 4
提问by Chris Till
Ok, so about a year ago I wrote a web app that helps organize appointments for my dads company. He now "couldn't do business without it". I have decided that I want to build a SAAS subscription model out of it and open it up to the public.
好的,大约一年前,我编写了一个网络应用程序,可以帮助我为我父亲的公司安排约会。他现在“没有它就无法做生意”。我决定用它构建一个 SAAS 订阅模型并向公众开放。
It's currently built on codeigniter and php which I do not think is a good fit for a SAAS version. I am planning on rebuiling it from scratch in laravel 4 and using stripe as a payment gateway.
它目前建立在 codeigniter 和 php 上,我认为这不适合 SAAS 版本。我计划在 laravel 4 中从头开始重新构建它,并使用 Stripe 作为支付网关。
My concern is how best to handle the database / application structure for more than one client. Currently, it just serves the one business and is very un-abstract and is specific to my dads companies needs. I need it to be able to handle different data depending on what the business who uses it does.
我关心的是如何最好地处理多个客户端的数据库/应用程序结构。目前,它只服务于一项业务,非常不抽象,专门满足我父亲的公司需求。我需要它能够根据使用它的业务的用途处理不同的数据。
I have looked into multi-tenancy but i'm not sure this is right for this. I am thinking that a 'gmail' style approach would be better. One app / domain that after login the user will see their customised dashboard and only their data.
我已经研究了多租户,但我不确定这是否适合于此。我认为“gmail”风格的方法会更好。一个应用程序/域,用户在登录后将看到他们定制的仪表板和他们的数据。
Before I get stuck in with the coding I need to work out how best to handle multiple 'accounts' on the one database. I do not want to create a table for each user, nor a database for each user.
在我陷入编码之前,我需要弄清楚如何最好地处理一个数据库上的多个“帐户”。我不想为每个用户创建一个表,也不想为每个用户创建一个数据库。
I guess my question is can anybody point me in the right direction for how best to handle a monthly payment subscription in Laravel? It's not so much the code that I'm stuggling with, rather what exactly I would need to build to handle charging the customer each month and denying them access if billing failed etc.
我想我的问题是有人能指出我如何最好地处理 Laravel 每月付款订阅的正确方向吗?与其说是我在苦苦挣扎的代码,不如说是我究竟需要构建什么来处理每个月向客户收费并在计费失败时拒绝他们访问等。
Thanks
谢谢
回答by tplaner
You are in for a lot of reading and a ton of work!
你需要大量的阅读和大量的工作!
First of all, let's completely ignore the billing aspect of this for now — at the end of the day that portion of the application is really fairly trivial. Take a page out of 37signals Rework(page 93 and 94) and launch your product with a 30 day free trial before you even begin implementing it (you should know how to implement it by then).
首先,让我们暂时完全忽略此方面的计费方面——归根结底,应用程序的那部分确实相当微不足道。从37signals Rework(第 93 和 94页)中取出一页,并在开始实施之前以 30 天的免费试用期推出您的产品(届时您应该知道如何实施它)。
Second, why do you think that "gmail" doesn't use multi-tenancy, URI structure tells nothing about the underlying database structure. I'm fairly confident they aren't cloning a database schema for every one of their customers. Therefore you've probably answered your own question — you want to implement multi-tenancy.
其次,为什么你认为“gmail”不使用多租户,URI 结构没有告诉底层数据库结构。我相当有信心他们不会为他们的每一位客户克隆一个数据库模式。因此,您可能已经回答了自己的问题——您想要实现多租户。
You're going to want to abstract your database (and application architecture), and honestly there is no better resource to help you on your way to doing that than Taylor Otwell's (creator of Laravel) book Laravel: From Apprentice To Artisan. His book is not for beginners, and by the time you're done reading it you should probably be able to answer this question for yourself.
你会想要抽象你的数据库(和应用程序架构),老实说,没有比 Taylor Otwell(Laravel 的创造者)的书Laravel: From Apprentice To Artisan更好的资源来帮助你做到这一点。他的书不适合初学者,当你读完它时,你应该能够自己回答这个问题。
You are not going to be creating a table or a database for each user, you aren't even going to be creating one for each organization. Instead you'll be creating abstract database structure in code, which will pull your users data out of the database.
您不会为每个用户创建表或数据库,甚至不会为每个组织创建一个。相反,您将在代码中创建抽象的数据库结构,这将从数据库中提取您的用户数据。
Think about checking for permission to access an organization as another layer of user authentication. On every request you'll be checking to see if that user can access a particular organization. You'll likely also check to ensure that organization is still active (did it expire because they didn't pay?) this will again happen on every request and likely with a filter within laravel.
考虑检查访问组织的权限作为用户身份验证的另一层。对于每个请求,您都将检查该用户是否可以访问特定组织。您可能还会检查以确保组织仍然处于活动状态(是否因为他们没有付款而过期?)这将再次发生在每个请求中,并且可能在 laravel 中使用过滤器。
This really leads to the next very important factor of developing a SaaS application.
这确实导致了开发 SaaS 应用程序的下一个非常重要的因素。
I don't know about you, but I'm paranoid, and I couldn't sleep well at night if I wasn't sure that user number 4506
couldn't see the data of an organization that he doesn't belong to. The only really good way to ensure this is through unit testing, which I'd highly suggest learning if you haven't already.
我不了解你,但我是偏执狂,如果我不确定用户号4506
看不到他不属于的组织的数据,我晚上都睡不好。确保这一点的唯一真正好方法是通过单元测试,如果您还没有学习,我强烈建议您学习。
The best way to do this within Laravel 4 is to read Jeffrey Way's book Laravel Testing Decoded. This book is extremely advanced, but still easy to understand if you have a good grasp of the fundamentals.
在 Laravel 4 中执行此操作的最佳方法是阅读 Jeffrey Way 的书Laravel Testing Decoded。这本书非常先进,但如果您很好地掌握了基础知识,仍然很容易理解。
Last but not least, the number one thing is get involved in the community — the easiest way I'd suggest doing that is idling on the #laravel IRC channel(freenode). Ask some questions, maybe answer some questions, everyone in the channel is very nice and responsive.
最后但并非最不重要的一点是,第一件事是参与社区——我建议这样做的最简单方法是在#laravel IRC 频道(freenode)上闲置。问一些问题,也许回答一些问题,频道里的每个人都很好,反应灵敏。
You are definitely in for an adventure, don't be afraid to ask questions and make mistakes. Good luck.
您绝对是在冒险,不要害怕提问和犯错。祝你好运。
回答by MCannon
As a rough overview, I would have a clients table, and a subscriptions table. Any other data that needs storing such as contacts, or appointments, can be associated using foreign keys to the client table.
作为粗略的概述,我将有一个客户表和一个订阅表。任何其他需要存储的数据(例如联系人或约会)都可以使用外键关联到客户端表。
In laravel, you can use the ORM to get the currently logged in client, and then through a relationship, fetch appointments and contacts belonging to them.
在laravel中,你可以使用ORM来获取当前登录的客户端,然后通过一个关系,获取属于他们的约会和联系人。
There are some useful tools for laravel at cartalyst.com, including sentry and sentry-social for user auth, and integrating user accounts with facebook/google/twitter, etc.
在 cartalyst.com 上有一些用于 laravel 的有用工具,包括用于用户身份验证的 sentry 和 sentry-social,以及将用户帐户与 facebook/google/twitter 集成等。
Stripe will allow you to configure recurring payments, and will notify you via web hooks each time there is a payment attempt. you can log these in the payments table, and associate them with a user/client. you can use this to keep track of who has paid, and how recently.
Stripe 将允许您配置定期付款,并在每次有付款尝试时通过网络挂钩通知您。您可以将这些记录在支付表中,并将它们与用户/客户相关联。您可以使用它来跟踪谁付款以及最近付款时间。
Also, bear in mind that you may not want to cancel the account immediately on failed payment.
另外,请记住,您可能不想在付款失败后立即取消帐户。
Stripe will reattempt, and it may be that your best response is after it is two or three days late, or you get an invalid card notification,to get in touch with the client and prompt them to update their payment details.
Stripe 会重新尝试,您最好的回复可能是在延迟两三天后,或者您收到无效的卡通知,以与客户联系并提示他们更新他们的付款详细信息。
It may also be an opportunity to check when they last logged in. If it was over a month ago you can credit them with a free month, and remind them of how much your app can do for them. By doing this, you may be able to get people to continue using (and paying) for something they had forgotten they had subscribed to.
这也可能是查看他们上次登录时间的机会。如果是在一个月前,您可以给他们一个免费月,并提醒他们您的应用程序可以为他们做多少。通过这样做,您可以让人们继续使用(并支付)他们忘记订阅的东西。