CodeIgniter 中国开发者社区诚征热爱 CI 的版主

查看完整版本: kohana文档翻译(Loading resources)

星期八 2008-7-16 15:09

kohana文档翻译(Loading resources)

[indent]自Kohana 2.1.2 $this→load() 不被赞成使用
[/indent][b]自动加载[/b]
如果某个类没有被加载,PHP具有自动加载的功能,Kohana也有此功能,举例:
[code=PHP]$user = new User_Model;
$cache = new Cache();
html::stylesheet();
$this->layout = new View('layout');[/code]


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

[code=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();[/code]

[b]加载数据库(Loading database)[/b]
除了前面提到的方法,加载数据库还可以这样$this->load->database();
注意在模型里database会自动加载
[b]加载辅助函数(Loading helpers)[/b]
[indent]加载辅助函数用$this→load→helper() 将不被赞成,它不会工作,并在LOG中引发一个调试错误
[/indent]使用辅助函数非常简单,仅访问静态方法,例如echo url::base();
[b]加载视图[/b]
视图是Kohana的最后输出,在页面中它们可以相互植入,不止一种方法加载视图,
[code=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);[/code]

[b]加载模型[/b]
加载模型的方法也不止一种,更多的方法可以在 Models中找到.
例如你的模型User_Model,文件名models/user.php,在控制器中加载模型,例如
[code=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);[/code]

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


不用的选项用逗号隔开.
注意有些类总是自动加载:URI, Router 和 Input.
页: [1]
查看完整版本: kohana文档翻译(Loading resources)