| 状态 | 草稿 |
|---|---|
| Todo | GET support |
| 官方最后更新时间 | 2008/12/06 15:25 |
URLs 在 Kohana 的是分段组成部分。一个典型的URL:http://localhost/control/action/arg1/arg2
其中各分段依次对应控制器,控制器方法,方法参数。
例如
http://localhost/index.php?/articles/edit/1/my-first-article // 使用同样的URL并重写 http://localhost/articles/edit/1/my-first-article
分段解析:
首先第一个分段是 articles 控制器,其文件是在application/controllers/articles.php - 关于控制器(控制器(Controllers))请看这里。
其次第二个分段对应 application/controllers/articles.php 文件中 Articles_Controller 类的 edit 方法。如果第二个分段不存在则调用 index() 方法,.如果还不存在则调用 _ _call() 方法或显示404错误。
第三个分段和第四个分段则是 edit() 方法的两参数,比如:edit($id,$title)
再举一个例字,让大家看看通过代码如何实现。
实例
class Articles_Controller extends Controller { function __construct(){ parent::__construct(); } function index() { } function edit($id,$title){ //从数据库获取文章并编辑 echo $id; $this->load->view('articles/edit'); } }
就像上面所所的, Kohana urls 是分段组成的。
例如
http://localhost/articles/edit/1/my-first-article分段部分
使用 URI 库 类和 URL 辅助函数 提供的方法可以是编码工作变得更为简单。您可以获取分段,确定当前的URL部分,以及其他方法。
默认情况下 Kohana urls 中包含 index.php。 不但不好看而且也不利于搜索引擎的抓取。大家请分别看下面两个不同的URLs:
http://localhost/index.php/articles/edit/1/my-first-article // 使用同样的URL并重写 http://localhost/articles/edit/1/my-first-article
后者的不仅好看也利于SEO,(译者注:这就是所谓的优化SEO,或友好化SEO)。
这里有一个使用 Kohana 的示例 .htaccess 文件,它已经包含在框架里面。
Kohana 允许设置 urls 的后缀。
例如
http://localhost/index.php?/articles/edit/1/my-first-article.html设置方法在 application/config/config.php 文件的 **url_suffix** 参数中。
查询字符串和 GET 方法在 Kohana 中是可选的。你可以简单的在 URL 后附加 ?var=value,当 global_xss 开启时,传入的参数对会被输入类库进行检测和过滤 。