从Revel Framework中的模块继承控制器

I am trying to use a shared controller from a module in my app, but I'm not really sure how to do it. Here's what I want to do:

  1. I have two revel apps, a frontend and backend app. The frontend app is used to show the user-facing site, and the backend app is for admin stuffs.
  2. I created a special controller to connect to database as per the booking sample.
  3. I want both the frontend and backend app to use the same controller, to minimize redundancy.

From the sample, when you want to have one controller database, it roughly translate to this:

type DBController {
     *revel.Controller
}

type App {
    DBController
}

This works when I want to have only 1 app, but when I want to share the controller to another app, I can't import DBController to the app.

Things I've Tried

I tried moving DBController to its own package, and then importing that and inherit from it directly:

// in db.go
package controllers

// import and stuffs

type DBController {
    *revel.Controller
}

// in app.go
package controllers
import (
    dbc "site.com/modules/controllers"
)

type App struct {
    dbc.DBController
    // *dbc.DBController
}

This gives me a panic error stating that the route is not found:

panic: Route validation error (in /app/path/routes:7):
       revel/controller: failed to find controller App

in both inheriting with and without pointer.

I've also tried Revel's module, with the same code, but different directory and importing via config:

// app.conf
modules.dbcontroller=site.com/modules/dbcontroller

And then in app.go:

type App struct {
    DBController
}

But it still didn't work with the same error as before. I'm pretty convinced that the right route is by using module, since the documentation said (emphasis mine):

Modules are packages that can be plugged into an application. They allow sharing of controllers, views, assets, and other code between multiple Revel applications or from third-party sources.

A module should have the same layout as a Revel application’s layout. The “hosting” application will merge it in as follows:

  1. Any templates in module/app/views will be added to the Template Loader search path
  2. Any controllers in module/app/controllers will be treated as if they were in your application.

etc..

But I'm not sure how I can share and derive my controller from here.

TL; DR

How do I share controller in Revel so that I can inherit a controller from other module, roughly like:

import dbc "site.com/modules/dbcontroller"

type App struct {
    dbc.DBController
}

so that DBController can be used with several revel apps? Thank you very much.

dongpang1232
dongpang1232 你找到解决方案了吗?我正在尝试做同样的事情。
一年多之前 回复

1个回答



我不是权威机构,但是即使我没有一个完整的答案,我也会提出一些意见,但可能会对您有所帮助。 </ p>

关于您的问题的第一件事使我震惊的是术语“继承”的使用-Go不支持继承。 它确实支持嵌入。 我可能将问题主题写为“从Revel框架中的模块重用控制器。</ p>

其次,我想知道您是否正在尝试在两个单独的Revel应用程序之间重用Revel模块? 尝试重用 one </ em> Revel应用程序的两个独立部分中的模块代码,而这些应用程序恰好具有前端和后端。快速阅读揭示框架使我认为模块是为 第三,我想知道您是否正在将文件与包混淆。在学习Go时,对我来说并不明显,一个包可以跨越多个文件。 </ b>
</ div>在两个文件(例如db.go和app.go)中存在相同的“程序包控制器”声明。</ p>
</ div>

展开原文

原文

I am not an authority, but I will make a few observations that might help even though I do not have a complete answer for you.

The first thing about your question that struck me is the use of the term "inherit" -- Go does not support inheritance. It does support embedding. I might have written the question subject as "Reuse controller from module in Revel framework.

Second, I wonder if you are trying to reuse a Revel module between two separate Revel applications or if you are trying to reuse code from a module in two separate parts of one Revel application that just happens to have a front end and a back end. A quick read of the reveal framework makes me think modules were designed for the former, not the latter.

Third, I wonder if perhaps you are confusing files with packages. It was not obvious to me when learning Go that one package can span multiple files.. if the same declaration of "package controller" exists in two files such as db.go and app.go, they are still in the same package.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐