用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4242|回复: 0
收起左侧

kohana文档翻译(Controllers)

[复制链接]
发表于 2008-7-16 15:21:01 | 显示全部楼层 |阅读模式
控制器的命名和解析:
一个控制器基本上可以是任意文件名,控制器的类(class)名必须与文件名一致.
控制器的约定:
1.必须在controllers目录下.
2.控制器的文件名必须小写, e.g. articles.php
3.控制器的类名必须是控制器首字母大写的文件名附加上_Controller,比如Articles_Controller
4.必须继承Controller类
5.以”_”线开头的方法(如_do_something() )不能被URL的参数访问
我们创建一个简单的控制器在application/controllers/article.php,将会输出”hello world”.
现在你在浏览器键入www.yoursite.com/article 你将会看到

PHP复制代码
class Article_Controller extends Controller{
    public function index(){
        echo ‘Hello World!;
    }
}
复制代码



现在你在浏览器键入www.yoursite.com/article 你将会看到:
Hello World
更多高级控制器
在上面的示例中,index()方法被URL:www.yoursite.com/article访问,如果URL的第二部分为空的话,index()方法会自动载入,上例也能被下例URL访问:www.yoursite.com/article/index
如果URL的第二部分不为空.它决定控制器中哪一个访问被调用:application/controllers/article.php
PHP复制代码
class Article_Controller extends Controller{
    public function index(){
        echo 'Hello World!';
    }
    public function overview() {
        echo 'Article list goes here!';
    }
}
复制代码



现在你键入www.yoursite.com/article/overview 将会显示
Article list goes here!
控制器与URL片断
如果我们展现一篇特殊的文章,例如一篇标题为your-article-title,ID为1的文章,URL可能是www.yoursite.com/article/view/your-article-title/1,这个URL中的最后两部分将会传递到view()方法.
application/controllers/article.php
PHP复制代码
class Article_Controller extends Controller{
    public function index(){
        echo 'Hello World!';
    }
    public function overview(){
        echo 'Article list goes here!';
    }
    public function view($title,$id){
        echo $id . ' - ' . $title;
        // you'd retrieve the article from the database here normally
    }
}
复制代码


你访问www.yoursite.com/article/view/your-article-title/1将会显示
1 - your-article-title
控制器与子目录
如果你把一个控制器放到/controllers/下的一个子目录, Kohana 会根据控制器自动匹配,比如一个文件:application/controllers/admin/user.php对就URL:http://localhost/admin/user
控制器路由
出于某种原因,你的URL不能与控制的方法对就,你可以使用URL路由,关于URL路由的更多信息
特殊方法
index
当ULR中没有别的方法时,index()方法会被自动载入.比如访问Welcome_Controller控制器,你的URL是http://example.com/welcome,index()方法会被自动载入
_remap
如果你的控制器有此方法,那么你控制器的所有访问都会发到此方法.由原来URL决定的方法将会做为_remap方法的参数来处理.例如:
PHP复制代码
class Article_Controller extends Controller{
    public function _remap( $method, $data ){
       // method is the first part of the uri.
       // it'll be index if there is no segment following the controller name
       // or the segment if there is
 
       // the $data param is an array of the remaining uri segments (if any)
       // otherwise it is an empty array
    }
}
复制代码



www.domain.com/article中,method=index,data=none
www.domain.com/article/read中,method=read,data=none
domain.com/article/read/1中,method=read,data=array(’1′)
在这个示例中你也可以不用_remap方法.关于此方法的更多信息
http://learn.kohanaphp.com/2008/03/26/using-_remap/
_default
_default()在控制器被访问的方法不存在的时候会被载入.例如在URL:http://example.com/welcome/sthrandom90923中,sthrandom90923方法不存在,_default()会被调用.比如很容易的触发404页面.URL中的方法和参数将这样访问_default($method, $arguments)
私有方法
有时候你希望你的方法不被展现在WEBSITE中,只让它们在控制器内部能被访问,你可以把方法申明为private或者在方法名前加’_'前缀.
application/controllers/home.php
PHP复制代码
class Article_Controller extends Controller
{
    public function index()
    {
        echo 'Hello World!';
    }
    private function _article_form()
    {
        echo 'Article form';
    }
}
复制代码



当你访问www.yoursite.com/article/_article_form 时,将显示404页面
构造函数
如果你要定义一个构造函数,你必须先调用父类的构造方法application/controllers/home.php
PHP复制代码
class Article_Controller extends Controller
{
    protected $db;
    protected $session;
 
    public function __construct()
    {
        parent::__construct(); //this must be included
 
        $this->db = new Database;
        $this->session = new Session;
    }
 
    public function index()
    {
        $this->session->get('user_id');
    }
 
    public function view($article_id)
    {
        $article_id = (int) $article_id;
        $article = $this->db->getwhere('articles', array('id' => $article_id));
    }
}
复制代码



在这个实例中.向我们展示了如何从数据库中检索出文章.如果你输入www.yoursite.com/article/view/5 ,它将会在数据库查找ID为5的文章
特殊的控制器
使用基控制器
在你的程序中使用基控制器非常有用处,你可以在任何页面执行,比如进行用户认证和授权,以及SESSION处理非常有用.
Example MY_Controller.php

PHP复制代码
<?php
class Controller extends Controller_Core
{
    public function __construct()
    {
        parent::__construct();
 
        // authentication goes here for example
    }
 
    public function do_something()
    {
        // method available in all controllers
    }
}
?>
复制代码

这个文件可以命名为MY_Controller.php,可以放到application/libraries下,这个类具有基类的所有方法加上你自己定义的方法
注意,MY_前缀可以在application/config/config.php文件中配置$config['extension_prefix'] 参数

本版积分规则