Hi I am new to lagom and macwire. I am learning lagom. Here I am trying to use playframework as front-end . In my frontend controller I am using MessagesAbstractController
to build form. I wired HomeController
in Frontend
But I am failing to compile this front-end play sub-project. It says Cannot find a value of type: [play.api.mvc.MessagesControllerComponents] [error] lazy val homeController = wire[HomeController]
I have overcome this problem by manually instantiating MessagesControllerComponents
But, I want to know that how can I solve this problem by macwiring
.
Here is the code–
package controllers
import com.example.lagomwithplay.api.LagomwithplayService
import javax.inject._
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc._
import scala.concurrent.Future
case class UserData(name: String, age: Int)
class HomeController @Inject()(
cc: MessagesControllerComponents,
helloService: LagomwithplayService
) extends MessagesAbstractController(cc) {
val userForm = Form(
mapping(
"name" -> text,
"age" -> number
)(UserData.apply)(UserData.unapply)
)
def searchPage() = Action.async { implicit request =>
Future.successful(Ok(views.html.searchFormPage(userForm)))
}
def search(id: String) = Action.async { implicit request =>
userForm.bindFromRequest().fold(
hasErrors = (errorForm) => Future.successful(Ok(views.html.searchFormPage(errorForm))),
success = (data) => {
Future.successful(Ok(userForm.bindFromRequest().data.toString()))
}
)
}
}
APPLICATION LOADER:
import _root_.controllers.{AssetsComponents, HomeController}
import com.example.lagomwithplay.api.LagomwithplayService
import com.lightbend.lagom.scaladsl.api.ServiceLocator.NoServiceLocator
import com.lightbend.lagom.scaladsl.api.{LagomConfigComponent, ServiceAcl, ServiceInfo}
import com.lightbend.lagom.scaladsl.client.LagomServiceClientComponents
import com.lightbend.lagom.scaladsl.devmode.LagomDevModeComponents
import com.softwaremill.macwire.wire
import play.api.ApplicationLoader.Context
import play.api._
import play.api.http.FileMimeTypes
import play.api.i18n.{I18nComponents, Langs, MessagesApi}
import play.api.libs.ws.ahc.AhcWSComponents
import play.api.mvc._
import play.filters.HttpFiltersComponents
import router.Routes
import scala.collection.immutable
import scala.concurrent.{ExecutionContext, Future}
abstract class Frontend(context: Context) extends BuiltInComponentsFromContext(context)
with HttpFiltersComponents
with I18nComponents
with AhcWSComponents
with AssetsComponents
with LagomServiceClientComponents with LagomConfigComponent {
override lazy val serviceInfo: ServiceInfo = ServiceInfo(
"frontend",
immutable.Seq(ServiceAcl.forPathRegex("(?!/api/).*"))
)
override implicit lazy val executionContext: ExecutionContext = actorSystem.dispatcher
import play.api.routing._
override def router: Router = {
val prefix: String = "/"
wire[Routes]
}
lazy val homeController = wire[HomeController]
//Consuming services
lazy val helloService = serviceClient.implement[LagomwithplayService]
}
class FrontendLoader extends ApplicationLoader {
override def load(context: Context): Application = context.environment.mode match {
case Mode.Dev =>
new Frontend(context) with LagomDevModeComponents {
}.application
case _ =>
new Frontend(context) {
override def serviceLocator = NoServiceLocator
}.application
}
}
MANUALLY OBJECT CREATION
import _root_.controllers.{AssetsComponents, HomeController}
import com.example.lagomwithplay.api.LagomwithplayService
import com.lightbend.lagom.scaladsl.api.ServiceLocator.NoServiceLocator
import com.lightbend.lagom.scaladsl.api.{LagomConfigComponent, ServiceAcl, ServiceInfo}
import com.lightbend.lagom.scaladsl.client.LagomServiceClientComponents
import com.lightbend.lagom.scaladsl.devmode.LagomDevModeComponents
import com.softwaremill.macwire.wire
import play.api.ApplicationLoader.Context
import play.api._
import play.api.http.FileMimeTypes
import play.api.i18n.{I18nComponents, Langs, MessagesApi}
import play.api.libs.ws.ahc.AhcWSComponents
import play.api.mvc._
import play.filters.HttpFiltersComponents
import router.Routes
import scala.collection.immutable
import scala.concurrent.{ExecutionContext, Future}
abstract class Frontend(context: Context) extends BuiltInComponentsFromContext(context)
with HttpFiltersComponents
with I18nComponents
with AhcWSComponents
with AssetsComponents
with LagomServiceClientComponents with LagomConfigComponent {
override lazy val serviceInfo: ServiceInfo = ServiceInfo(
"frontend",
immutable.Seq(ServiceAcl.forPathRegex("(?!/api/).*"))
)
override implicit lazy val executionContext: ExecutionContext = actorSystem.dispatcher
import play.api.routing._
override def router: Router = {
val prefix: String = "/"
wire[Routes]
}
// needs to created after router initialization
lazy val messagesControllerComponents = new MessagesControllerComponents {
override def messagesActionBuilder: MessagesActionBuilder = new MessagesActionBuilder {
override def parser: BodyParser[AnyContent] = defaultBodyParser
override def invokeBlock[A](request: Request[A], block: MessagesRequest[A] => Future[Result]): Future[Result] =
block.apply(new MessagesRequest[A](request, messagesApi))
override protected def executionContext: ExecutionContext = controllerComponents.executionContext
}
override def actionBuilder: ActionBuilder[Request, AnyContent] = controllerComponents.actionBuilder
override def parsers: PlayBodyParsers = controllerComponents.parsers
override def messagesApi: MessagesApi = controllerComponents.messagesApi
override def langs: Langs = controllerComponents.langs
override def fileMimeTypes: FileMimeTypes = controllerComponents.fileMimeTypes
override def executionContext: ExecutionContext = controllerComponents.executionContext
}
lazy val homeController = wire[HomeController]
//Consuming services
lazy val helloService = serviceClient.implement[LagomwithplayService]
}
class FrontendLoader extends ApplicationLoader {
override def load(context: Context): Application = context.environment.mode match {
case Mode.Dev =>
new Frontend(context) with LagomDevModeComponents {
}.application
case _ =>
new Frontend(context) {
override def serviceLocator = NoServiceLocator
}.application
}
}