Introduction To Routes

Introduction to Routes

What and Why

Akelos工具native PHP rewriting,因此把URL站点解析的责任从webserver转移到Akelos自身。由于这些原因这个特性被添加。

  1. 不是所有的webservers支持重写。通过把代码转移到Akelos框架上,在所有的webservers能够执行“out of the box”功能。
  2. 由Akelos生成的静态的URLs,是用于search-engine和human friendly的设计。
  3. 在Akelos中可写的实施一样被用来生成定制的URLs,链接到标准的URL例如 url_for, link_to, and redirect_to.

Akelos我们想象一下在你的server中mod_rewrite是可用的。如果他不可用,那么http://example.com/post/all 将被 http://example.com/?ak=post/all会替代。如果你知道mod_rewrite 是可用的,你应该在你的config/config.php中定义AK_ENABLE_URL_REWRITE为真来避免mod_rewrite自动检测的开销。

Diving In

路径已经被the average PHP programmer设计为完全可定制的了。你不再需要为你的站点有一个漂亮的URLs而费劲心机成为一个.htaccess wizard。

Routes在”config/routes.php”中被定义。这是一个典型的PHP source文件,其包含的文档类似于

$Map->connect('/', array('controller' => 'page', 'action' => 'index'));

那么代替无谓的冗长和平淡,让我们正确的潜入吧。

Example 1—Setting A Default Controller

让我们来说一下,你是这些使用cool kids中的一个,你喜欢这样来显示你最近的发表 “http://www.your-cool-domain.com/“。

Case 1 - BlogController::index()

你的BlogController’s index action为显示最近的posts设置好了。为了指定默认的controller,我们在默认路径中添加'controller' ⇒ ‘blog’:

$Map->connect('/:controller/:action/:id', array('controller' => 'blog'));

As an additional perk, Routes will opt for the shortest path possible, so “url_for :controller ⇒ ‘blog’, :action ⇒ ‘index’” will generate a url such as “http://www.your-cool-domain.com/”.

Case 2—BlogController#recent

让我假设一下,无论因为什么原因,你的BlogController用其他的action显示的最近posts。我们需要添加一个新的路径,来匹配一个空的路径和指向BlogController’s “recent” action。

$Map->connect('/', array('controller' => 'blog', 'action' => 'recent'));
$Map->connect('/:controller/:action/:id', array('controller' => 'page', 'action' => 'index'));

注意我们在默认路径之前添加了一个新路径。路径是可以识别的并且在他们被定义的顺序中被生成。在Akelos默认路径之前铺设我们定制的路径,我们能确认”url_for :controller ⇒ ‘blog’, :action ⇒ ‘recent’” 会为我们用空路径生成一个URL,例如 “http://www.your-cool-domain.com/”.

Example 2—Setting up date-based URLs

假如你想给你blog添加一个特性,这个特性是让你的访问这按时间顺序阅览posts。你喜欢让他们案年月日的方式浏览,并且你已经有一个action,这个aciong使用查询或者发送名字为year, month, 和 action的变量。

对于这个理想的URL是”http://www.your-cool-domain.com/2005/02/14”,用month和day组件是可选的。因为你是一个健康的人,你决定date的顺序是YYYY/MM/DD, 远比那些不容易动的 MM/DD/YYYY好的多。

再者,我们想在默认的Akelos route之前添加我们定制的路径。第一步也许是

$Map->connect('/:year/:month/:day', array(
  'controller' => 'blog', 
  'action' => 'by_date',
));

虽然这个完全匹配”http://www.your-cool-domain.com/date/2005/02/14“,但是对于http://www.your-cool-domain.com/date/2005/02确是失败的 - 我们需要标记”:month”和”:day” 作为可选的组件。为了这么做我们在我们的route添加”'month' ⇒ OPTIONAL”:

$Map->connect('/:year/:month/:day', array(
  'controller' => 'blog', 
  'action' => 'by_date',
  'month' => OPTIONAL,
  'day' => OPTIONAL,
));

现在我们的可定制的路径可以识别这样的URLs,例如”http://www.your-cool-domain.com/date/2005/02” 或者这样的”http://www.your-cool-domain.com/date/2005“。在这个翻转的面上,当使用标准的forms helpers例如link_to和url_for生成URLs时,我们的可定制的路径也会自动的被使用。

现在我们进入另一个问题—我们的新路径太一般了,并且他紧紧的抓住URLs的三元素—例如”posts/show/10”。为了与之抗争,我们必须知道放置在我们的路径组件上的一些请求。请求被放置在$Map→connect的第三个参数上。如下:

$Map->connect('/:year/:month/:day', array(
    'controller' => 'blog', 
    'action' => 'by_date',
    'month' => OPTIONAL,
    'day' => OPTIONAL,
    'year' => COMPULSORY),
  // Requirements
  array(
    'year'=>'/(20){1}\d{2}/',
    'month'=>'/((1)?\d{1,2}){2}/',
    'day'=>'/(([1-3])?\d{1,2}){2}/'
  )
);

Example 3 — Posts by Category

让我们返回到你的BlogController。假设你的每一个posts属于一个”category”,并且我们喜欢基于category之上显示每一个条目。例如”http://www.your-cool-domain.com/posts/akelos“。让我们假设你同样使用了category “all”来显示所有的categories。快速的路径应该是:

$Map->connect('/posts/:category', array('controller' => 'blog', 'action' => 'posts'));

这个路径的问题是你的”link_to”语句将要包含”:category ⇒ ‘all’”。如果你的templates包含的话,那将非常的漂亮

  <%= link_to 'Posts', :controller => 'blog', :action => 'posts' %>

通过给这个路径一个默认值”:category”,我们可一个达到目的:

$Map->connect('/posts/:category', array('controller' => 'blog', 'action' => 'posts', 'category' => 'all'));

An added bonus is that the URL generated for the above “link_to” will omit the extraneous ‘all’ component, and be displayed as “http://www.your-cool-domain.com/posts”.

 
routes_cn.txt · Last modified: 2008/07/15 10:13 by liyh
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki