指定路由规则,并路由到不同的组件 [英] Specify route rules, and route to different components

查看:92
本文介绍了指定路由规则,并路由到不同的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 Mason :: Plugin :: RouterSimple ,例如,给定的URL为:

I know how to to specify routes for page components using Mason::Plugin::RouterSimple, for example given a url of:

/archives/2015/07

我可以这样创建组件archives.mc:

<%class>
  route "{year:[0-9]{4}}/{month:[0-9]{2}}";
</%class>
Archives for the month of <% $.month %>/<% $.year %>

同样,我可以创建一个news.mc组件来处理以下网址:

and similarly I can create a news.mc component that will handle urls of:

/news/2012/04

很好(而且非常优雅!),但是现在我想要的是能够处理以下URL:

and that's fine (and very elegant!) but now what I want is to be able to handle urls like the following ones:

/john/archives/2014/12
/john/news/2014/03
/peter/news/2015/09
/bill/archives/2012/06

等我知道我可以将路由规则写为:

etc. I know I can write the route rules as:

<%class>
  route "{user:[a-z]+}/archives/{year:[0-9]{4}}/{month:[0-9]{2}}", { action=> 'archives' };
  route "{user:[a-z]+}/news/{year:[0-9]{4}}/{month:[0-9]{2}}", { action=> 'news' };
</%class>

,但是请求必须由两个不同的组件处理.如何将请求路由到其他组件? Mason不会匹配archives.mcnews.mc,因为在组件名称之前有一个用户名.

but then the requests have to be handled by two different components. How can I route a request to different components? archives.mc and news.mc won't be matched by Mason because there's a username before the name of the component.

推荐答案

问题是,虽然/archives/2014/12这样的urs可以由/archives.mc组件轻松处理,但对于/john/archives/2014/12/bill/archives/2012/06这样的网址,不清楚将存档组件放在何处.

The problem is that, while urs like /archives/2014/12 can be easily handled by an /archives.mc component, for urls like /john/archives/2014/12 and /bill/archives/2012/06 it's not clear where to put the archives component.

梅森将尝试匹配以下组件(这是简化列表,请参见

Mason will try to match the following components (it's a simplified list, please see Mason::Manual::RequestDispatch):

...
/john/archives.{mp,mc}
/john/dhandler.{mp,mc}
/john.{mp,mc}

但最后...

/dhandler.{mp,mc}

所以我的想法是在根目录中放置一个dhandler.mc组件:

So my idea is to put a dhandler.mc component in the root directory:

<%class>
  route "{user:[a-z]+}/archives/{year:[0-9]{4}}/{month:[0-9]{2}}", { action=> 'archives' };
  route "{user:[a-z]+}/news/{year:[0-9]{4}}/{month:[0-9]{2}}", { action=> 'news' };
</%class>
<%init>
  $m->comp($.action.'.mi', user=>$.user, year=>$.year, month=>$.month);
</%init>

如果该网址与第一个路由匹配,它将调用archives.mi组件:

If the url matches the first route, it will call the archives.mi component:

<%class>
  has 'user';
  has 'year';
  has 'month';
</%class>
<% $.user %>'s archives for the month of <% $.month %>/<% $.year %>

(我使用了.mi组件,因此只能在内部访问).

(I used a .mi component so it will be accessible only internally).

可以改进dhandler(更好的regexp,可以从数据库表中检查用户并拒绝请求等)

The dhandler can be improved (better regexp, can check users from a database table and deny the request, etc.)

由于我的档案和新闻组件可以接受POST/GET数据,并且由于我想接受任何数据,因此我可以通过以下方式传递所有内容:

Since my archives and news components can accept POST/GET data, and since I want to accept any data, I can just pass everything with:

 $m->comp($._action.'.mi', %{$.args});

不太老套,但看起来它确实可以正常工作.

Not too elegand, but it looks like it does its work.

这篇关于指定路由规则,并路由到不同的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆