백엔드 개발 PHP 튜토리얼 PHP HMVC框架kohana 总结1

PHP HMVC框架kohana 总结1

Jun 13, 2016 am 10:49 AM
config controller name nbsp

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中:
 

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控制层文件再继承之,十分好用
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

해결 방법: 조직에서 PIN 변경을 요구합니다. 해결 방법: 조직에서 PIN 변경을 요구합니다. Oct 04, 2023 pm 05:45 PM

해결 방법: 조직에서 PIN 변경을 요구합니다.

Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경 Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경 Sep 22, 2023 am 11:37 AM

Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경

Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까? Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까? Sep 14, 2023 pm 03:33 PM

Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까?

Windows 11/10 복구의 OOBELANGUAGE 오류 문제 Windows 11/10 복구의 OOBELANGUAGE 오류 문제 Jul 16, 2023 pm 03:29 PM

Windows 11/10 복구의 OOBELANGUAGE 오류 문제

Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법 Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법 Sep 15, 2023 pm 03:57 PM

Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법

Windows 11의 디스플레이 크기 조정 가이드 Windows 11의 디스플레이 크기 조정 가이드 Sep 19, 2023 pm 06:45 PM

Windows 11의 디스플레이 크기 조정 가이드

Windows 11에서 밝기를 조정하는 10가지 방법 Windows 11에서 밝기를 조정하는 10가지 방법 Dec 18, 2023 pm 02:21 PM

Windows 11에서 밝기를 조정하는 10가지 방법

Safari에서 iPhone의 개인 브라우징 인증을 끄는 방법은 무엇입니까? Safari에서 iPhone의 개인 브라우징 인증을 끄는 방법은 무엇입니까? Nov 29, 2023 pm 11:21 PM

Safari에서 iPhone의 개인 브라우징 인증을 끄는 방법은 무엇입니까?

See all articles