Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.5.X: method current in object Play is deprecated: This is a static reference to application, use DI instead

I'm new to PlayFramework.

Please give me a sample how to access Configuration parameters from my view. I'm using PlayFramework 2.5.3

Old way (@current is deprecated):

@current.configuration.getString("environment.param")

New way (as far as I understand, Configuration should be injected):

I know how to access it from controller.

@Inject() (val messagesApi: MessagesApi, configuration: Configuration)

How do I use it from my view?

like image 491
Mike Pakhomov Avatar asked Dec 18 '22 17:12

Mike Pakhomov


1 Answers

Sadly there isn't much you can do about it. It is just how it is when DI was introduced to Play, not much was discussed in regard to templates. One possible solution could be:

  1. Inject Configuration in controller
  2. Send it as implicit to your view/template

    class Application @Inject() (implicit val config: Configuration) extends Controller {
    
        def index = Action {
            Ok(views.html.index("foo"))
        }
    }
    

And your template will look like:

@(myParam1: Any)(implicit config: Configuration)
<h2>Some HTML here @myParam1 @config.getString("environment.param")</h2>

I am totally aware that this somehow defeats the purpose of DI but this is just how it is right now.

like image 191
Anton Avatar answered May 18 '23 15:05

Anton