简体   繁体   English

无法使用haxe.Web.Dispatch进行默认操作

[英]Unable to get default working with haxe.Web.Dispatch

I am currently setting up a website using haxe targetting php and I'm having a problem with the haxe.Web.Dispatch library. 我目前正在使用haxe定位php建立网站,而haxe.Web.Dispatch库存在问题。

Everything was working well until I tried to implement a doDefault() rule. 在我尝试实现doDefault()规则之前,一切工作正常。

I have the following rules in my dispatch api: 我的调度API中有以下规则:

doIndex(){ ... }

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

And these will both redirect to the correct webpage. 这些都将重定向到正确的网页。 For example these both work fine: 例如,这些都可以正常工作:

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

And now I've implemented 现在我已经实施

doDefault() {...}

in order to redirect any other urls to a 404 page, but it's not working. 为了将其他任何网址重定向到404页面,但无法正常工作。 Going to the above URLs still works fine but going to 转到上面的URL仍然可以正常工作,但是

http://foo.com/bar

gives the following error 给出以下错误

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}

The Dispatch documentation says 调度文件

In case the corresponding method doXXXX is not found on the api object, or if the URL is /, the action doDefault is used instead. 如果在api对象上找不到对应的方法doXXXX,或者如果URL是/,则使用操作doDefault。 An exception DispatchError.DENotFound("XXXX") is thrown if there is no default action (XXXX here being the placeholder for the URL part name). 如果没有默认操作(此处的XXXX是URL部件名称的占位符),则抛出DispatchError.DENotFound(“ XXXX”)异常。

but it doesn't say anything about the DETooManyValues exception. 但它并没有说明DETooManyValues异常。 Anyone have any ideas? 有人有想法么?

The DETooManyValues error is thrown if the URL you are dispatching has more parts than the matching action. 如果您分派的URL的部分多于匹配操作, DETooManyValues引发DETooManyValues错误。

So if you have: 因此,如果您有:

doPage( name:String );

Then by default "/page/aboutus/" will work, but "/page/aboutus/2/" will not. 然后默认情况下,“ / page / aboutus /”将起作用,但“ / page / aboutus / 2 /”将不起作用。 This applies for doDefault() too - "/" will work, "/bar" will not (by default). 这也适用于doDefault() -“ /”将起作用,“ / bar”将不起作用(默认情况下)。

The trick to getting this to work is using a "Dispatch" argument. 使它起作用的技巧是使用“ Dispatch”参数。

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}');
}

If Dispatch knows that your action/method has this dispatch argument, then it assumes your method knows how to deal with the extra values, and will no longer throw the error. 如果Dispatch知道您的操作/方法具有此dispatch参数,则它假定您的方法知道如何处理额外的值,并且将不再抛出错误。 You can use the d.parts array to access the extra parts. 您可以使用d.parts数组访问其他部分。

Added bonus: 增加的奖励:

You can also use the d:Disaptch argument to: 您还可以将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()); 

I have a blog post which explains some more stuff if you're interested: 我有一篇博客文章,如果您感兴趣的话,还可以解释一些其他内容:

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

Also feel free to keep asking questions, always keen to help someone else using Haxe for websites :) 也可以随时询问问题,总是热衷于帮助使用Haxe的网站的其他人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM