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

kohana文档翻译(Loading resources)

[复制链接]
发表于 2008-7-16 15:09:48 | 显示全部楼层 |阅读模式
自Kohana 2.1.2 $this→load() 不被赞成使用
自动加载
如果某个类没有被加载,PHP具有自动加载的功能,Kohana也有此功能,举例:
PHP复制代码
$user = new User_Model;
$cache = new Cache();
html::stylesheet();
$this->layout = new View('layout');
复制代码



这不同于Kohana自动加载功能,Kohana的自动加载功能会在任一页面加载某一个库,模型等,例如你想自动加载Session库在任意页面.
加载库(Loading libraries)
一般来说,有两种方式加载库,例如加载session库,

PHP复制代码
$this->load->library('session');
// the object can be approached with $this->session e.g.
echo $this->session->id()
// this is the CodeIgniter compatible approach
 
// another possiblity using  PHP5's nifty auto_load function
$session = new Session;
echo $session->id();
复制代码


加载数据库(Loading database)
除了前面提到的方法,加载数据库还可以这样$this->load->database();
注意在模型里database会自动加载
加载辅助函数(Loading helpers)
加载辅助函数用$this→load→helper() 将不被赞成,它不会工作,并在LOG中引发一个调试错误
使用辅助函数非常简单,仅访问静态方法,例如echo url::base();
加载视图
视图是Kohana的最后输出,在页面中它们可以相互植入,不止一种方法加载视图,
PHP复制代码
$this->layout=new View('layouts/layout'); // will load the file views/layouts/layout.php
//alternate syntax
$this->layout=$this->load->view('layouts/layout');
 
//will render the view
$this->layout->render(TRUE);
复制代码


加载模型
加载模型的方法也不止一种,更多的方法可以在 Models中找到.
例如你的模型User_Model,文件名models/user.php,在控制器中加载模型,例如
PHP复制代码
$user = new User_Model;
$name = $user->get_user_name($id); //get_user_name is a method defined in User_Model
 
// alternative syntax
$this->load->model('user');
$name = $this->user->get_user_name($id);
复制代码


预装载资源
这个功能不再赞成使用,自Kohana 2.2不再支持
在application/config/config.php ,你可以设置哪些库,哪些模型被自己动加载
PHP复制代码
$config['preload'] = array
(
    'libraries' => 'database, session',
    'models'    => 'book'
)
复制代码



不用的选项用逗号隔开.
注意有些类总是自动加载:URI, Router 和 Input.
发表于 2010-3-3 11:09:50 | 显示全部楼层
CI怎么自动加载呢?
请教

本版积分规则