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.

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题