如何在 Kohana 3 项目中安排业务逻辑

发布于 2024-08-25 15:33:50 字数 983 浏览 5 评论 0原文

我正在寻找有关如何使用 Kohana 3 设置中型 Web 应用程序的建议、教程和链接。我过去已经实现了 MVC 模式,但从未针对“正式”MVC 框架工作过,所以我仍然得到我的熟悉术语 - 尝试基本示例、构建视图和模板等。

我进展得相当不错,但我想建立一个现实世界的网络项目(我自己的一个项目,我已经计划了一段时间了)作为学习对象。

我通过示例学习得最好,但是现在 Kohana 3 基于示例的文档有点稀疏 - 他们自己在网站上这么说。虽然我并不担心在学习过程中学习框架,但我想确保代码库从一开始就结构良好 - 即控制器很好地划分,命名良好并符合标准,最重要的是业务逻辑是分成适当大小的模型。

我的应用程序的核心可以被描述为一个具有一系列搜索和列表功能的企业目录,以及每个条目所有者的登录区域。实际的管理数据库后端已经处理完毕。

假设我已经制定并准备好了所有 API - 列出所有企业、编辑企业、按街道名称列出企业、创建以企业身份登录的报价等等,我只是在寻找如何将功能融入其中MVC 模式并转换为可轻松扩展的 Kohana 应用程序结构。

  • 您知道现实生活中“数据库密集型”应用程序的示例吗,例如目录、在线社区……带有基于 Kohana 3 构建的登录区域(最好是开源的),这样我就可以看看他们是如何做到这一点的?

  • 是否有关于如何在 Kohana 项目中为最终用户构建可扩展登录区域的约定或最佳实践,该登录区域不仅能够处理业务目录页面,还能够处理单独页面上的其他产品?

  • 您知道有关使用 Kohana 构建复杂应用程序的任何好的资源吗?

  • 您是否构建了类似的东西并且可以给我有关项目结构的建议?

赏金

我向 @antpaw 提供赏金,因为他为我提供了一个 Kohana 应用程序,其中包含一些业务逻辑,为我提供了很多示例。也为@Pixel Developer 的出色贡献干杯 - 和往常一样,我希望有人能分享一份赏金!

I'm looking for advice, tutorials and links at how to set up a mid-sized web application with Kohana 3. I have implemented MVC patterns in the past but never worked against a "formalized" MVC framework so I'm still getting my head around the terminology - toying around with basic examples, building views and templates, and so on.

I'm progressing fairly well but I want to set up a real-world web project (one of my own that I've been planning for quite some time now) as a learning object.

I learn best by example, but example-based documentation is a bit sparse for Kohana 3 right now - they say so themselves on the site. While I'm not worried about learning the framework as I go along, I want to make sure the code base is healthily structured from the start - i.e. controllers are split nicely, named well and according to standards, and most importantly the business logic is separated into appropriately sized models.

My application could, in its core, be described as a business directory with a range of search and listing functions, and a login area for each entry owner. The actual administrative database backend is already taken care of.

Supposing I have all the API worked out and in place already - list all businesses, edit business, list businesses by street name, create offer logged in as business, and so on, and I'm just looking for how to fit the functionality into a MVC pattern and into a Kohana application structure that can be easily extended.

  • Do you know real-life examples of "database-heavy" applications like directories, online communities... with a log-in area built on Kohana 3, preferably Open Source so I could take a peek how they do it?

  • Are there conventions or best practices on how to structure an extendable login area for end users in a Kohana project that is not only able to handle a business directory page, but further products on separate pages as well?

  • Do you know any good resources on building complex applications with Kohana?

  • Have you built something similar and could give me recommendations on a project structure?

Bounty

I'm awarding the bounty to @antpaw because he provided me with a Kohana application with some business logic that is giving me a lot of examples. Cheers @Pixel Developer for your excellent input as well - as so often, I'd wish one could split a bounty!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2024-09-01 15:33:50

这里有很多问题需要回答,我会尽力回答。

您知道“数据库密集型”应用程序的真实示例吗?例如目录、在线社区……带有基于 Kohana 3 构建的登录区域,我可以在其中查看它们是如何实现的?

有一些示例应用程序。 Woody Gilk(Kohana 创始人)已将代码发布到他的个人网站 github 上。他为登录区域分配了一个 cookie 值。 Kohana 3 / 2.4 签署 cookie,使其安全并消除会话要求。这可能不符合每个人的口味,因此您始终可以使用同时使用会话和 cookie 的内置身份验证库。

以下是您可能感兴趣的其他一些项目:

  • Shindig - kohana 3 的轻量级博客模块
  • Kohanut - 用 Kohana 3 编写的可扩展 CMS

是否有关于如何在 Kohana 项目中为最终用户构建可扩展登录区域的约定或最佳实践,该登录区域不仅能够处理业务目录页面,还能够处理单独页面上的其他产品?

如果我理解正确的话,您想为每个页面生成一个登录框吗?这对于 Kohana 3 来说很容易,因为我们可以利用 HMVC 中的 H。 Sam de Fressyinet 在 iBuilding 技术博客上撰写了一篇文章,详细介绍了这一切。 使用 HMVC 扩展 Web 应用程序

然后,您可以做的是向登录控制器或操作执行内部请求,并将响应转储到您的视图页面中。

$login = Request::factory('login')->execute()->response;

$login 现在包含登录表单,您可以将其放在您喜欢的任何位置。如果请求是内部请求,您可能希望返回不同的响应,这就是这段代码有用的原因:

if (Request::instance() !== $this->request)
{
    print 'Internal called made with Request::factory';
}

您知道有关使用 Kohana 构建复杂应用程序的任何好的资源吗?

不会有文档向您展示如何构建复杂的应用程序。 Kohana 社区的观点是,您是一名 PHP 开发人员,应该能够自己解决这些问题。如果你不能,那么你就不应该使用 Kohana。

您是否构建了类似的东西并且可以给我有关项目结构的建议?

一旦您了解了 Kohana 3 如何查找文件,事情就很容易理解了。

|- classes
|-- controller
|-- model
|- views

例如:

Controller_Mathew extends Controller 

将在以下位置查找名为 mathew.php 的文件:

classes/controller

下划线可用于指定更深的目录。示例:

Controller_Mathew_Davies extends Controller

将在以下位置查找名为 davies.php 的文件:

classes/controller/mathew/

如您所见,控制器名称中的下划线充当目录分隔符。这对于模型和普通类来说都是正确的。

Lots of questions to answer here, I'll try my best.

Do you know real-life examples of "database-heavy" applications like directories, online communities... with a log-in area built on Kohana 3 where I could take a peek how they do it?

There's a few example applications out there. Woody Gilk (Kohana founder) has published the code to his personal website on github. For the login area he assigns a cookie value. Kohana 3 / 2.4 sign the cookies which makes it safe and removes the requirement for sessions. This might not be up to everyone's tastes so you can always use built in authentication library that uses both sessions and cookies.

Here are some other projects you might be interested in:

  • Shindig - Light weight blog module for kohana 3
  • Kohanut - An extensible CMS written in Kohana 3

Are there conventions or best practices on how to structure an extendable login area for end users in a Kohana project that is not only able to handle a business directory page, but further products on separate pages as well?

If I understand you correctly you want to generate a login box for each of those pages? This is easy with Kohana 3 as we can take advantage of the H in HMVC. Sam de Fressyinet wrote an article detailing what this all about on the iBuilding Tech Blog. Scaling Web Applications with HMVC.

What you can then do is perform an internal request to the login controller or action and dump the response into your view page.

$login = Request::factory('login')->execute()->response;

$login now contains the login form, which you can put anywhere you like. You may want to return a different response if the request is internal which is why this piece of code can be useful:

if (Request::instance() !== $this->request)
{
    print 'Internal called made with Request::factory';
}

Do you know any good resources on building complex applications with Kohana?

There's not going to be documentation showing you how to build complicated applications. The view of the Kohana community is that you're a PHP developer and should be able to solve these problems for yourself. If you can't, well you shouldn't be using Kohana then.

Have you built something similar and could give me recommendations on a project structure?

Once you understand how Kohana 3 finds files things are easy to understand.

|- classes
|-- controller
|-- model
|- views

For example:

Controller_Mathew extends Controller 

Will look for a file called mathew.php in:

classes/controller

Underscores can be used to specify deeper directories. Example:

Controller_Mathew_Davies extends Controller

will look for a file called davies.php in:

classes/controller/mathew/

As you can see, the underscores in the controller name act as directory separators. This rings true for models and vanilla classes.

一直在等你来 2024-09-01 15:33:50

我会使用 kohana 附带的身份验证模块进行登录。这将为您提供角色表,您可以在其中设置可能的权限选项并将其与用户相关联。之后,您可以在每个控制器的 __constructor() 或 action_function() 内部检查用户是否具有所需的角色,例如使用 ->has() 函数。您还应该使用 ORM 模块,它非常棒,因为表之间有很多关系。 ORM 对象内的 __get() 方法也非常方便。

通过将新参数设置为 NULL 并在 if 语句中检查该参数,扩展控制器功能也非常容易。例如,您只需要一个功能来编辑旧条目或添加新条目。

public funciton action_manage($id = NULL)
{
    $entry = ORM::factory('entry', $id); // if id is null a new entry will be returned 
}

将视图构建到子文件夹中以避免混乱的视图目录也很重要。

i would use the auth module that comes with kohana for the login. This will give you the roles table where you can setup the possible permission options and relating them to the users later. After that you can check inside the __constructor() or action_function() of each controller whether the user has the required role e.g. with the ->has() function. You also should use the ORM module, its just awesome, since you have many relations between the tables. Also the __get() method inside an ORM object can be extremely handy.

Its also pretty easy to extend a controller function by setting the new parameter to NULL and checking for that in a if statement. e.g. you need only one function for editing a old entry or adding a new one.

public funciton action_manage($id = NULL)
{
    $entry = ORM::factory('entry', $id); // if id is null a new entry will be returned 
}

It's also important that you structure the views into sub folders to avoid a messy view directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文