1

我目前正在使用以 php 为目标的 haxe 建立一个网站,但我遇到了haxe.Web.Dispatch库的问题。

在我尝试实施 doDefault() 规则之前,一切都运行良好。

我的调度 api 中有以下规则:

doIndex(){ ... }

doPosts(){y:String, m:String, n:String){ ... }

这些都将重定向到正确的网页。例如,这些都可以正常工作:

http://foo.com/index
http://foo.com/posts/2013/01/post-title

现在我已经实施了

doDefault() {...}

为了将任何其他 url 重定向到 404 页面,但它不起作用。转到上述 URL 仍然可以正常工作,但要

http://foo.com/bar

给出以下错误

uncaught exception: DETooManyValues

in file: C:\wamp\www\website\bin\lib\haxe\web\Dispatch.class.php line 191
#0 C:\wamp\www\website\bin\lib\Index.class.php(9): haxe_web_Dispatch->runtimeDispatch(Object(_hx_anonymous))
#1 C:\wamp\www\website\bin\lib\Index.class.php(12): Index->__construct()
#2 C:\wamp\www\website\bin\index.php(9): Index::main()
#3 {main}

调度文档

如果在 api 对象上没有找到相应的方法 doXXXX,或者如果 URL 是 /,则使用操作 doDefault 代替。如果没有默认操作(此处 XXXX 是 URL 部分名称的占位符),则会引发异常 DispatchError.DENotFound("XXXX")。

但它没有说明 DETooManyValues 异常。有人有想法么?

4

1 回答 1

1

如果您正在调度的DETooManyValuesURL 的部分比匹配的操作多,则会引发错误。

因此,如果您有:

doPage( name:String );

然后默认情况下“/page/aboutus/”会起作用,但“/page/aboutus/2/”不会。这也适用doDefault()- “/” 会起作用,“/bar” 不会(默认情况下)。

让它工作的诀窍是使用“调度”参数。

doPage( name:String, d:haxe.web.Dispatch ) {
    trace('Get page $name, with other parts: ${d.parts}');
}
doDefault( d:haxe.web.Dispatch ) {
    trace('Get page $name, with other parts: ${d.parts}');
}

如果Dispatch知道你的动作/方法有这个 dispatch 参数,那么它假定你的方法知道如何处理额外的值,并且不会再抛出错误。您可以使用d.parts数组来访问额外的部分。

额外奖励:

您还可以使用该d:Disaptch参数:

// Redirect to a different page, same get/post parameters
d.redirect("/differentpage/", d.params); 

// Redirect to a different controller.  If this is in /doDefault/, the whole URL is passed to the sub-controller.  
// If it is in `doPage` and the URL is /page/some/other/part, only `/some/other/part` will be passed on.
d.dispatch(new SomeOtherController()); 

如果您有兴趣,我有一篇博客文章会解释更多内容:

http://jasononeil.com.au/2013/05/29/creating-complex-url-routing-schemes-with-haxe-web-dispatch/

也可以随时提出问题,总是热衷于帮助其他人使用 Haxe 建立网站 :)

于 2013-08-07T23:38:08.903 回答