Play Framework: split routes in multiple files without sub projects
Asked Answered
N

3

26

My play project is massive and the routes file is approx 1Mb. Now, when scala compiles, I have the exception "Method code too large" because of the routing and the reverse routing scala files created from my routes file(that are big too).

So, I need to split my routes file without subprojects. Indeed, I can't split my project into subprojects because its components are interdependent.

I tried 2 methods:

  • I added a new conf file called technical.routes, add some routes inside, remove the same routes from "routes", and import the file with "-> technical.Routes" Everything compiles, I don't have my previous exception, but something is wrong because when it stops compiling, it starts over and over... and never ends.

  • I added a new conf file called technical.routes, add some routes inside, remove the same routes from "routes", but instead of importing it in my main routes file, I added it in the conf file: "application.router="routes, technical.routes"". But it's not working because only one route must be declared here.

How to do, please?

Nordau answered 26/11, 2013 at 8:19 Comment(1)
Well, the first method is working. I started from scratch and it worked. I did a clean command before the compile command. It seems that old compiled files were the cause of my problem.Nordau
N
26

Well, the first method is working. I started from scratch and it worked. I did a clean command before the compile command. It seems that old compiled files were the cause of my problem.

Be careful to note that you cannot have an overlap in package names in the routes files. E.g. in this example, the technical.routes file contains all routes in the controllers.technical and the main routes file cannot contain any routes in the controllers.technical package.

conf/routes contents:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

->  /technical technical.Routes

GET     /        controllers.Dashboard.index()

conf/technical.routes contents:

# Routes
# ~~~~

GET     /        controllers.technical.App.index()
Nordau answered 26/11, 2013 at 17:12 Comment(6)
Great! FYI, your answer would be better with a bit more code / a good example.Hollyanne
This did not work for me. Would you kindly provide a sample of your routes file?Pelfrey
You need to add this in your build.sbt to make multiple routes files working scalacOptions ++= Seq( // Show warning feature details in the console "-feature", // Enable routes file splitting "-language:reflectiveCalls" )Alpine
Not Working for me getting below error [error] /home/conf/routes:7: object Routes is not a member of package com.store [error] -> /storeDetails com.store.RoutesDufrene
I do nothing special to my build.sbt and it worked for me right away. (I've just spent two days chasing compiler errors after carving up a project and was mortified when it finally compiled and yet failed to show the sub-projects routes -- so thank you for this post): Question where is this documented? I've read everything on playframework.com/documentation/2.4.x/SBTSubProjects and yet this magical incantation does not appear there?Aprilaprile
Note that split routes files must go into their own package or you will get errors (github.com/playframework/playframework/issues/4430). A working example: github.com/Enalmada/play-beanstalkIdun
P
3

If your file name is technical.routes, while including the file, mention technical.Routes (caps).

The parameter after -> is the url prefix, so to access any url of technical.routes file, you need to add the prefix.

http://127.0.01/technical/{defined url in technical.routes file}

Penholder answered 17/11, 2015 at 12:34 Comment(0)
L
1

None of the above info worked for me in play 2.8.x and macwire. Sharing the solution that worked for me.

If you are using a sbt single project and DI and still want to use multiple routes file, you can do so like below. No need of sbt multi project setup.

conf/ 
     routes
     admin.routes

conf/routes:

GET /index                  controllers.HomeController.index()

->  /admin admin.Routes

conf/admin.routes:

GET /index                  controllers.admin.HomeController.index()

And in the application loader, add below into build routes.

val adminRouter: admin.Routes = {
        val prefix = "/"
        wire[admin.Routes] //replace it with constructor if you do manual DI
}
val router: Routes = {
        val prefix = "/"
        wire[Routes] //replace it with constructor if you do manual DI
}

Tested with play 2.8.x and macwire.

Lanettelaney answered 16/7, 2021 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.