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.
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.