1

I have a basic app going and I want users to be redirected to the main base action if an action they try to reach is not in the controller.

I have some routes set up:

addRoute(name="pinShow",        pattern="pin/show/[key]",       controller="pin",       action="show");
addRoute(name="pinShow",        pattern="pin/show",             controller="pin",       action="show");
addRoute(name="pin",            pattern="pin",                  controller="pin",       action="index");

At the moment, if the user types /pin/something/ in the URL, the action loaded is index, but is stays on this URL. I want to do a 301 redirect and not simply load in the default action view, etc.

I thought I'd be able to do this with the verifies function in my controller, but it doesn't seem to be working.

verifies( except="index,show", handler="invalidAction" );

With my handler being:

private void function invalidAction() {

    flashInsert( messages = [{ messageString="Invalid action.", messageType="info" }] );
    redirectTo(route="pin");

}

None of this is working. Any idea's anyone?

I'm about to go live and it's just something I want to tighten up! I'd rather kill a link with a 404 or redirect rather than have the default action loaded in.

ale
  • 6,369
  • 7
  • 55
  • 65
Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140

2 Answers2

3

You can change your routes to look like this:

addRoute(name="pinShow", pattern="pin/show/[key]", controller="pin", action="show");
addRoute(name="pinShow", pattern="pin/show",       controller="pin", action="show");
addRoute(name="pin",     pattern="pin/[invalid]",  controller="pin", action="invalidAction");
addRoute(name="pin",     pattern="pin",            controller="pin", action="index");

In this case, you may need to make the invalidAction method public for this to work.

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
1

@Michael Giovanni Pumo you can use onmissingtemplate event of wheels and there you can write a function to redirect to home page

Keshav jha
  • 1,356
  • 8
  • 22