2014-09-24 49 views
1

我想在Catalyst应用程序的URL末尾使用后缀来确定响应所采用的格式。我可以重写提供给Catalyst的URL吗?

因此http://foo/bar.json会导致json响应,http://foo/bar.xml在xml中,而http://foo/bar在纯HTML页面中。

我正在尝试在最初的区块中执行此操作 - 在beginauto操作中 - 通过重写用于后续操作的调度URL,但似乎不起作用。

有关于此的建议?包括反对意见 - 毕竟这可能不是一个好主意。

谢谢 -

+1

如何制作“http:// foo.com/bar/json”,“http:// foo.com/bar/xml”等形式的网址,然后使用此解决方案:http: //stackoverflow.com/questions/4408408/what-is-the-best-way-to-hanlde-optional-url-arguments-in-a-catalyst-controller – 2014-09-24 08:40:16

回答

1
sub type : Regex('foo/bar(\.[^/]+)?') { 
    my ($self, $c) = @_; 
    my ($type) = @{ $c->req->captures }; 
    $c->log->info("Type: ".$type); 
    $c->response->body($c->welcome_message); 
} 

你可以试试上面的代码,现在基于你将不得不切换视图以JSON/XML/HTML的$类型。

相关问题