I don't understand why this isn't working.
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Serve index page from public directory
GET / controllers.HomeController.index
GET /index controllers.FrontendController.index
# An example route (Prefix all API routes with apiPrefix defined in application.conf)
GET /api/summary controllers.HomeController.appSummary
POST /api/getplt/:args controllers.UploadController.getPlt(args)# Serve static assets under public directory
GET /*file controllers.FrontendController.assetOrDefault(file)
Why can't it resolve the action? I can resolve the /api/summary without problems.
First for your assets use the following:
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(file)
Then you could use the assetFinder within your views, something like:
<script src="#assetsFinder.path("javascripts/jquery.js")" type="text/javascript"></script>
This is because try to avoid any similar/identical routes, your first route and the asset related route, looks almost the same.
So to answer your question, either the Play is mixing these two routes, or you dont have index method within your HomeController class.
Related
I have encountered a situation where the routes are not being generated. In my play service I had situation where all the routes were defined in application routes file as follows:
GET /foo packageA.ControllerA.methodA()
… lots of other endpoints under ControllerA
GET /bar packageB.ControllerB.methodB()
… lots of other endpoints under ControllerB
GET /baz packageC.ControllerC.methodC()
… lots of other endpoints under ControllerC
When the application is the compiled, the reverse routes are generated.
Because the application routes file was getting too big, it was decided to split out the routes into separate routes file, one for each package like this:
routesForA.routes
GET /foo packageA.ControllerA.methodA()
… lots of other endpoints under ControllerA
etc
The reverse routes are again generated fine.
However, we wanted to put one endpoint from packageA.ControllerA into the routes file for packageB.ControllerB.
Say we moved
packageA.ControllerA.methodA into the routes file for packageB.controllerB.
Then what happened is that the reverse route for ControllerA.methodA was generated but all the other reverse routes for ControllerA routes (defined in the packageA routes file) were not generated.
Is this a bug or feature?
Our main routes file looks a bit different. We delegate in the main routes file to the special ones.
routes:
-> /foo foo.Routes
So in this example all requests that starts with /foo are forwarded to the foo.routes file (also in the conf folder)
foo.routes would look then like
GET /methodB packageB.ControllerB.methodB()
So the url for this would be /foo/methodB
I have Swagger UI for API documentation, I use the same approach like in official specification for accessing it I use next URL:
http://localhost:9000/docs/swagger-ui/index.html?url=/assets/swagger.json
But I want to use http://localhost:9000/docs/ instead. I won't want to use WS for delegating, I would like to use single line in routes, like this:
GET /docs controllers.Assets.at(path:String="/public/lib/swagger-ui", file:String="index.html?url=/assets/swagger.json")
Or
GET /docs controllers.Assets.at(path:String="/public/lib/swagger-ui", file:String="index.html")
and http://localhost:9000/docs?url=/assets/swagger.json
What shold I change so it work?
You can't make shortness in route file for the URL /docs/swagger-ui/index.html?url=/assets/swagger.json because index.html is generated by swagger-ui plugin to public directory and requires access to files nearby (like js and css files). Play swagger-ui uses javascript for fetching json based description of your routes via URL parameter for further parsing this document to swagger-ui, in your case it's /assets/swagger.json endpoint.
I tried to make the mapping swagger's index.html file, so pass json location like URL parameter directly:
GET /swagger-ui controllers.Assets.at(path = "/public/lib/swagger-ui", file = "index.html")
Play couldn't render this page, and CSS wasn't found. I appended dummy mapping to every file in swagger's default directory /public/lib/swagger-ui:
GET /*file controllers.Assets.at(path = "/public/lib/swagger-ui", file)
Even after that Play couldn't properly render index.html.
How it can be solved without Play-Swagger:
Create directory public\swagger in your project;
Download all files from https://github.com/swagger-api/swagger-ui/tree/master/dist and copy them to public\swagger;
Copy your swagger.json (it's specification, right?) to public\specification;
Add to your routes file next line:
GET /docs
controllers.Assets.versioned(path="/public/specification", file: Asset
= "swagger.json")
I have created the sub routes from main route and I have created the subcontroller package e.g controllers.subcontrollers. Now how to access my sub routes from sub controllers.
You can access by using this:
// Redirect to /hello/Bob
def helloBob = Action {
Redirect(routes.Application.hello("Bob"))
}
As you can see in the playframework documentation.
https://playframework.com/documentation/2.3.x/ScalaRouting
But, if you meant that you have multiples controllers and divided them into packages and want to access them inside another another controller. You can achieve it using something like this:
controllers.packageName.routes.ControllerName.functionInsideController
Try to create a new route ex: subcontrollers.routes
main routes Ex:
-> /subcontrollers subcontrollers.Routes
subcontrollers.routes Ex:
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.subcontrollers.Assets.at(path="/public", file)
GET /example controllers.subcontrollers.SubController.sample(parameter1:Long,parameter2: String)
Reference Link
I have defined the following routes file:
GET / controllers.Application.app
# web service entries...
GET /api/users controllers.Users.list
[...]
# Map static resources from the /public folder to the /assets URL path
GET /*file controllers.Assets.at(path="/public", file)
and in my Application.app action I just redirect to index.html
def app = Action {
Redirect(routes.Assets.at("index.html"))
}
so when I access to my application at http://mydomain I'm redirected to http://mydoamin/index.html, which then add a /#ideas to the location (it's a single web page app)
I'd like to get rid of the index.html file, and instead being redirected to http://mydoamin/#ideas
is it possible?
Use this in your routes file:
GET / controllers.Assets.at(path="/public", file="index.html")
So you need not to touch any controller.
Just a pointer: you can try to get the Html code from the Asset controller, and then return an Ok Result with the Html content.
For that, you may try to look at how controllers are called in the test helpers, and how to get the Result and body of this call.
Roughly (not tested):
def app = Action {
request => {
val result = controllers.Assets.at("public","index.html")(request)
val html = contentAsString(result)
Ok(html)
}
}
Are there some proper Zend methods for:
a) receiving path to /public directory
b) receiving application url
Actually I'm using methods defined in Controller, but it feel right to use ready methods if they exits.
protected function _getApplicationUrl() {
return $_SERVER['SERVER_NAME'];
}
protected function _getPublicPath() {
return realpath(APPLICATION_PATH . '/../public/');
}
Regarding the application URL, Zend_Controller_Request_Http has a getRequestUri() method, but it deliberately (and somewhat frustratingly) excludes the scheme and hostname parts of the URL. In my apps I have resorted to grabbing $_SERVER['HTTP_HOST'] in the bootstrap and storing it in the registry so that I can use it later when constructing full URLs.
And from memory, no, there isn't any built-in method to get the location of the public folder, but the code you have is fine. Most apps I've seen define() all the paths in index.php, which I suppose is slightly safer (only because the path names get set sooner and become absolutely immutable) and ever so slightly faster, but lets not get into a debate about micro-optimizations! :-)
1) If your virtual host point to ZF /public then in View you can get path by helper method $this->baseUrl(); In controller $this->view->baseUrl(); Otherwise create your own helper and use it.
2) In controller $this->getRequest()->getHttpHost();
protected function _getPublicPath() {
chdir(APPLICATION_PATH);
return realpath("../public");
}
a) receiving path to /public directory
Built-in php-function getcwd() will give you the path to your site-host folder (ex. output "/home/my_cp/public_html/my_site.loc/www"). And then, you can construct whatever path you want.