Play 2.1.x default catch all route - scala

Is there a default catch all fallback route in Play framework? If so how can I configure it in my routes file? Currently, there are some urls that I do not want the user to call them and even if they call, I don't want that error page appearing, rather I want them to go to the landing page of my web app! Is there a way to do that in the routes configuration file?

Simply define a route matching any path at the end of your routes file. Do not forget to define specific routes for your assets though, for example:
GET / controllers.Application.index
GET /some/path controllers.Application.someHandler
...
# End of file
GET /favicon.ico controllers.Assets.at(path="/public", file="img/favicon.ico")
GET /$file<(css|img|js|partials)/.*> controllers.Assets.at(path="/public", file)
GET /$path<.*> controllers.Application.catchall(path)
Any URL not matched by an earlier rule will be matched by this one.

Catch all routes makes sense when you want to do something with the path (ie. resolve it manually in your custom action), otherwise it's enough to use common onHandlerNotFound in your Global object and redirect request wherever you want.

Related

Writing the uri pattern in the url

I'am using play framework 2.6 with scala, I want to prevent accessing my web pages from url .In the usual case if i write the URI pattern in the url it will redirct to the web page by executing the associated action ,for example if i write in the url : http://localhost:9000/home it will redirect to the home page, but what i want is to redirect to error page rather than the home page when writing an URI (in this case: /home) in the url.
my routes file
GET /home controllers.HomeController.index()
You can redirect in the controller method:
# my routes file
GET /home controllers.HomeController.errorRedirect()
# HomeController
...
def errorRedirect = Action {
Redirect("/error")
}
or perhaps you want to catch all and then redirect:
# my routes file
GET /home controllers.HomeController.home()
GET /*path controllers.HomeController.errorRedirect()
See also: supplying a custom error handler in the play docs.

How facebook like websites is able to load the profile, instead of a directory when a request like facebook.com/profile/username is recieved?

When the facebook.com/profile/{username} is requested how is server able to load page with data corresponding to that user, instead of navigating to a directory named in that {username}, and possibly showing a 404 error ?
It's achieved typically using a pattern called "front controller", where all requests are handled by the same file (let's say index.php, talking specifically about PHP now). So all URLs are like this:
facebook.com/index.php/profile/abc
facebook.com/index.php/account
That file serves as the bootstrap for the application, reading extra parameters (anything after index.php) and dispatching requests to the appropriate handlers/controllers.
Then there's multiple ways you can get rid of that ugly index.php, depending on how you configure your web server (loads of questions here about that subject: htaccess remove index.php from url as an example).
Read more about it here: https://en.m.wikipedia.org/wiki/Front_controller

Play Framework 2.4 routing: one route for multiple urls Scala

I have admin part in my project which use React.js and React router.
And I need to render the same view on each request which starts with /admin in case user hit Refresh or come straight to url.
I don't need to pass any parameters to view. How can it be done?
You can define your route like this in play:
/admin/*url controllers.Application.admin(url)

Zend framework - duplication in translated url

I have these URLs
cz/kontroler/akce
en/controller/action
Is used transatable route and works it like charm. But problem is, that when you will write
cz/controller/akce
it works as well.
In generally when you have
cz/something-in-czech
en/something-in-english
which route to someController, will be works still
cz/some
en/some
because it is really name of controller.
How solve this duplicity content issue?
You can create a plugin that uses a preDispatch method. Before the request is executed, you can analyze the url requested and check if the requested language match the language of the params in the url (I mean controller and action). If not, you can redirect the user to the url that is in accord to the given language (basically you'll translate the controller and the action and then redirect the user to the right url).

Redirect to same named page in directory structure before path changes

Well, say I have a number of html pages in my web. The case is that I´m doing changes sometimes in the directory structure, so when anybody try to access to a determinated URL, it's possible that such URL does not exit. The files names don't change but so do the paths.
As far as I now, the server takes the user to a "404" page that can be customized. Is possible to customize the page in this way?:
The user tries oneweb.com/oldpath/page.html; which does not exist.
A 404 customized page is launched
404 page runs an script IS THIS POSSIBLE?
The script is given the name of the file WHERE IS STORED SUCH NAME?
The script search the entire directory structure to find page.html HOW TO ACCESS TO THE STRUCTURE
The file is found and the new URL is stored: oneweb.com/newpath/page.html
a link appears showing the new URL
Maybe this process is relatively common and I can find some related code or tutorial?
Are you using Apache? Linux?
Add a 404 handler
ErrorDocument 404 /404.php
Then use 404.php to parse the url. This simple example just grabs everything after the last / in the URI so http://example.com/foo/bar/page.html would put page.html in $url:
$url = end(explode('/', $_SERVER['REQUEST_URI']));
Then use one of the comment example functions in http://php.net/manual/en/function.readdir.php to search your directory and find the file.
Then do a header 301 redirect
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: http://example.com/' . $file_path);