duanbei1709 2015-08-16 08:53
浏览 36

从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.

  • 写回答

1条回答 默认 最新

  • dounie5475 2015-08-16 10:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法