PHP HMVC框架kohana 总结1
程序员文章站
2024-04-06 12:55:07
...
PHP HMVC框架kohana 小结1
之前kohana 3的安装见:http://jackyrong.iteye.com/admin/blogs/1186006
1 在bootstrap中设置开发模式:
Kohana::$environment = Kohana::DEVELOPMENT;
再增加如下代码:
if (isset($_SERVER['KOHANA_ENV']))
{
Kohana::$environment = $_SERVER[?KOHANA_ENV?];
}
相关的值有:
PRODUCTION, STAGING, TESTING, and DEVELOPMENT.
2 设置时区
date_default_timezone_set('America/Chicago');
3 启用相关的module
Kohana::modules(array( 中的注释按需要打开
4 创建默认的config文件(这点比CI差了)
在application\config下设置一个比如site.php的文件,放置日常用的变量,比如:
return array(
'name' => 'Egotist',
'tag_line' => "Let's talk about me!"
);
则获得其变量时,这样:
$site_config = Kohana::config('site');
$site_name = $site_config['name'];
$tag_line = $site_config['tag_line'];
甚至可以这样只加载其中的某个变量:
$site_name = Kohana::config('site.name');
也可以数组的形式加载变量,比如:
return array(
'name' => 'Egotist',
'details' => array(
'tag_line' => "Let's talk about me!",
'alt_tag_line' => "Today's subject: ME!";
);
);
加载:
$site_config = Kohana::config('site');
// Echo site name and details
echo $site_config['name']; // Egotist
echo $site_config['details']['tag_line'] // Lets talk about me!
echo $site_config['details']['alt_tag_line'] // Today's subject: ME!
也可以:
echo Kohana::config('site.details.tag_line');
5 controller的命名规范,必须符合如下
Controller_xxxx,XXX放在classes/controller/xxx.php,比如
Controller_User_Profile则为 classes/controller/user/profile.php
6 给view传递数据
controller中:
public function action_index()
{
//3.2只能用这个方法
$view = View::factory('welcome')
->set('site_name', 'Egotist')
->set('random', rand(1,10));
$this->response->body($view);
}
view中:
is a number between 1 and 10
也可以用bind绑定
$view = View::factory('welcome')->bind('site_name', $site_name)
->bind('random', $random);
$site_name = 'Egotist';
$random = rand(1, 10);
$this->response->body($view);
7 使用template controller
class Controller_Welcome extends Controller_Template
{
$content = View::factory('welcome')
->bind('random', $random);
$random = rand(1, 10);
$content->site_name = 'Egotist Beta';
$this->template->content = $content;
}
}
页面中直接输出:
8 设置全局变量,以方便在各页面中直接读取
View::set_global('site_name', 'Egotist Beta');
之后可以在任何view中读取:
9 在控制层中,写一个基类,保存一些基本的信息,比如CSS,JAVASCRIPT,常量可以这
样:
abstract class Controller_Application extends Controller_Template {
public function before()
{
parent::before();
View::set_global('site_name', 'Egotist Beta');
$this->template->content = '';
$this->template->styles = array();
$this->template->scripts = array();
}
其他PHP控制层文件再继承之,十分好用
之前kohana 3的安装见:http://jackyrong.iteye.com/admin/blogs/1186006
1 在bootstrap中设置开发模式:
Kohana::$environment = Kohana::DEVELOPMENT;
再增加如下代码:
if (isset($_SERVER['KOHANA_ENV']))
{
Kohana::$environment = $_SERVER[?KOHANA_ENV?];
}
相关的值有:
PRODUCTION, STAGING, TESTING, and DEVELOPMENT.
2 设置时区
date_default_timezone_set('America/Chicago');
3 启用相关的module
Kohana::modules(array( 中的注释按需要打开
4 创建默认的config文件(这点比CI差了)
在application\config下设置一个比如site.php的文件,放置日常用的变量,比如:
return array(
'name' => 'Egotist',
'tag_line' => "Let's talk about me!"
);
则获得其变量时,这样:
$site_config = Kohana::config('site');
$site_name = $site_config['name'];
$tag_line = $site_config['tag_line'];
甚至可以这样只加载其中的某个变量:
$site_name = Kohana::config('site.name');
也可以数组的形式加载变量,比如:
return array(
'name' => 'Egotist',
'details' => array(
'tag_line' => "Let's talk about me!",
'alt_tag_line' => "Today's subject: ME!";
);
);
加载:
$site_config = Kohana::config('site');
// Echo site name and details
echo $site_config['name']; // Egotist
echo $site_config['details']['tag_line'] // Lets talk about me!
echo $site_config['details']['alt_tag_line'] // Today's subject: ME!
也可以:
echo Kohana::config('site.details.tag_line');
5 controller的命名规范,必须符合如下
Controller_xxxx,XXX放在classes/controller/xxx.php,比如
Controller_User_Profile则为 classes/controller/user/profile.php
6 给view传递数据
controller中:
public function action_index()
{
//3.2只能用这个方法
$view = View::factory('welcome')
->set('site_name', 'Egotist')
->set('random', rand(1,10));
$this->response->body($view);
}
view中:
Welcome to
is a number between 1 and 10
也可以用bind绑定
$view = View::factory('welcome')->bind('site_name', $site_name)
->bind('random', $random);
$site_name = 'Egotist';
$random = rand(1, 10);
$this->response->body($view);
7 使用template controller
class Controller_Welcome extends Controller_Template
{
$content = View::factory('welcome')
->bind('random', $random);
$random = rand(1, 10);
$content->site_name = 'Egotist Beta';
$this->template->content = $content;
}
}
页面中直接输出:
8 设置全局变量,以方便在各页面中直接读取
View::set_global('site_name', 'Egotist Beta');
之后可以在任何view中读取:
9 在控制层中,写一个基类,保存一些基本的信息,比如CSS,JAVASCRIPT,常量可以这
样:
abstract class Controller_Application extends Controller_Template {
public function before()
{
parent::before();
View::set_global('site_name', 'Egotist Beta');
$this->template->content = '';
$this->template->styles = array();
$this->template->scripts = array();
}
其他PHP控制层文件再继承之,十分好用
相关文章
相关视频
上一篇: 关于php cron任务管理的实现假想
推荐阅读
-
PHP HMVC框架kohana 总结1
-
CI框架中数据库操作函数$this->db->where()相关用法总结_php实例
-
攻克CakePHP(PHP中的Ruby On Rails框架)图文介绍第1/2页_PHP教程
-
kohana框架上传文件验证规则写法示例_php实例
-
Laravel框架数据库CURD操作、连贯操作总结,laravelcurd_PHP教程
-
PHP开发框架kohana中处理ajax请求的例子_php实例
-
经验总结 PHP框架常见错误_PHP教程
-
PHP的Yii框架中过滤器相关的使用总结
-
网友支招:PHP网站开发kohana框架里的几个写法
-
php kohana框架 view使用