UFront - Trouble with routing

76 views
Skip to first unread message

Domagoj Štrekelj

unread,
Dec 3, 2014, 3:35:26 PM12/3/14
to haxe...@googlegroups.com
Hello,

I apologise if this is not the right place to ask these sorts of questions, but I don't know where else to turn.

For the past few weeks I've been trying to figure out UFront. Guides are scarce and examples outdated, but the sources are extensively commented making the API a decent substitute for a manual. However, sometimes the most trivial undocumented bit of information can throw a spanner in the works, which is hopefully the case here.

While I easily got a "Hello, world!" controller to work, I'm experiencing difficulties setting up different routes for my web application. Here's an example of my Routes class, which I have configured as the indexController for my UfrontApplication object:

package app;

import ufront.web.Controller;
import ufront.web.result.ViewResult;

class Routes extends Controller
{
@:route("/*")
public function index()
{
return "index";
}

@:route("/more/*")
public function indexMore()
{
return "more";
}
}

The app successfully compiles to PHP. I'm using USBWebserver to host a local webserver. I did not do any additional configuration as USBWebserver comes preconfigured for PHP, and I did not write an .htaccess file for the app output.

When accessing "http://localhost:8080/haxe/ufront-example-HelloWorld/www/", which is where the PHP output is located, I see the "index" output as expected.

However, when I navigate to "http://localhost:8080/haxe/ufront-example-HelloWorld/www/more/" I receive an HTTP 404 error, with the message: "The requested URL /index.php/more/ was not found on this server."

I tried omitting the wildcards, but then I receive a UFront frowney-face error with the following message in the console log: "ufront.handler.ErrorPageHandler.handleError(53): Handling error: Error: Page Not Found app.Routes.execute:433".

My questions are:
  • How do I properly set up different routes?
  • What is the difference between routes with wildcards and those without?
I would be immensely grateful for any guidance you could offer.

Andreas Mokros

unread,
Dec 3, 2014, 6:18:29 PM12/3/14
to haxe...@googlegroups.com
Hi.

On Wed, 3 Dec 2014 12:35:26 -0800 (PST)
Domagoj Štrekelj <domagoj...@gmail.com> wrote:
> I did not write an .htaccess file for the
> app output.

I don't know ufront, but normally every web-app needs a .htaccess like
this:

RewriteEngine On
RewriteBase /path/to/your/basedir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Without that your routes will always result in 404s.

--
Mockey

Jason O'Neil

unread,
Dec 3, 2014, 7:44:11 PM12/3/14
to haxe...@googlegroups.com
Hi,

I've never used USBWebserver, so I'm not sure if it has the equivalent of ".htaccess" (on apache), but it looks like it does do rewriting, transforming "http://localhost:8080/haxe/ufront-example-HelloWorld/www/" into the URI "/index.php/more/".  Which is close enough to what we want.

(As a side note, you might find it easier to work with if you get USBWebserver to point directly to your "haxe/ufront-example-HelloWorld/www/" folder, which might make it less confusing than the URL rewriting it is doing.)

Here are my suggestions:

  1. Change your `index()` function to `return context.getRequestUri()`.  This will show you the URI that ufront is getting from USBWebServer.

    Ideally, we want to get "/" when you visit http://localhost:8080/haxe/ufront-example-HelloWorld/www/
    and "/more" when you visit http://localhost:8080/haxe/ufront-example-HelloWorld/www/more/

  2. When you create your UfrontApplication object, change some configuration values and see if you get a better URI.  For example:

    new UfrontApplication({
        urlRewrite: false, // I suspect setting this to false will help you
        basePath: "/haxe/ufront-example-HelloWorld/www/",  // Not sure if you will need this with USBWebServer?
        indexController: Routes
    });

  3. Once you get `Context.getRequestUri()` returning reasonable values, you are ready to get your routing working.  And that will involve fixing the wildcards...
Now, about the routes and wildcards
  • Wildcards match any route, so your "indexMore" will match "/more", "/more/", "/more/1" and "/more/1/2/3" etc.
    If the route was just `@:route("/more")`, with no wildcard, then it would match "/more" and "/more/", but not "/more/1" or "/more/1/2/3" etc.

  • Routes match in the order of the file.  In your case, it will first check the route of index, ("/*") before it checks the route of indexMore, ("/more/*"). Because index is a wildcard, it will match anything, and so index() will always be called, because even "/more/1" is a match for "/*".  So if you're going to have a "/*" route that matches everything, for example to do a 404 page, I strongly suggest you put it as the last route in your controller.

I hope that helps,

Jason


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Domagoj Štrekelj

unread,
Dec 4, 2014, 5:52:33 AM12/4/14
to haxe...@googlegroups.com, he...@jasono.co
Thank you both for your reply, Andreas and Jason!

Both of you have been immensely helpful. Following the steps Jason provided I successfully managed to set up different routes in my example project.

As suspected, the issue lied in the improperly configured base path of the web app. USBWebserver's 'server root' is located in the "root" directory of the USBWebserver package. With the basePath of the UFront application configured to "/", (to my understanding) it began searching for index.php at the very root of the... well, 'server root' directory. The reason why the wildcard route ( "/*") worked is because it matched the "haxe/ufront-example-HelloWorld/www/" path. Mystery solved.

I adjusted the UfrontApplication configuration parameters accordingly:

ufrontApp = new UfrontApplication({
 indexController
: app.Routes,
 basePath
: '/haxe/ufront-example-HelloWorld/www/',
});


The 'Context.getRequestUri()' calls now return the expected URI's: "/", and "/more" or "/more/".

It seems like an .htaccess file is not necessary as USBWebserver is configured to handle the rewriting on its own. However, I included one similar to Andreas' as insurance.

Now that I have a better understanding of the basePath field, I'm going to store it in a configuration file for easier adjustment. Once I finish this example and document it, I'll set up a GitHub repository and publish it there. I would really like to increase the amount of information avaialable on UFront, and feel like a few example projects are a good start.

Once again, thank you for your help! I hope I'll be able to return the favour soon,

Domagoj
Reply all
Reply to author
Forward
0 new messages