`
haohappy2
  • 浏览: 325852 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

how to develop site using kohana

 
阅读更多

Kohana Start Configuration

Step 1:

.htaccess

# This is a sample .htaccess file. Simply replace
# RewriteBase below with the url path to this directory.
#
# Example:
#  /shop/    - if an Apache alias points to ".../public/" (this folder)
#  /         - if this directory is reachable at the web root
#
# Server-Side includes
#
Options All
AddHandler server-parsed .scss
AddType text/css .scss

# enable rewrite engine
RewriteEngine On

SetEnv APP_ENVIRONMENT DEVELOPMENT
SetEnv APP_BASE_URL {your domain}
SetEnv APP_COUNTRY CN

#SetEnv WEB_PROXY XXXXXX
#SetEnv WEB_PROXY_PORT 8080
#SetEnv WEB_PROXY_USERPWD name:pass

# tell Apache for which URL rewrites should occur
# (the rules below are relative to this url)
RewriteBase /

# rewrite all requests which don't point to a file
# or an directory to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

# turn off magic quotes in PHP
# (http://www.php.net/manual/en/security.magicquotes.whynot.php)
php_flag magic_quotes_gpc off
php_flag magic_quotes_runtime off


Step 2:

open the module in bootstrap.php file

Kohana::modules(array(
    // 'auth'       => MODPATH.'auth',       // Basic authentication
     'cache'      => MODPATH.'cache',      // Caching with multiple backends
    // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
    'database'   => MODPATH.'database',   // Database access
    // 'image'      => MODPATH.'image',      // Image manipulation
    'orm'        => MODPATH.'orm',        // Object Relationship Mapping
    // 'unittest'   => MODPATH.'unittest',   // Unit testing
    'userguide'  => MODPATH.'userguide',  // User guide and API documentation
    ));

Step 3:
Need to configure the bootstrap.php

Load some configuration outside

Log Configuration:

if(Kohana::$environment === Kohana::PRODUCTION) {
    // we use syslog in production
    Kohana::$log->attach(new Kohana_Log_Syslog("patrick"), array(Log::ERROR, Log::INFO));
    define('_INFO', false);
    define('_DEBUG', false);
} else {
    /**
     * Attach the file write to logging. Multiple writers are supported.
     */
    Kohana::$log->attach(new Kohana_Log_File(APPPATH.'logs/default/info_log'), array(Log::INFO));
    Kohana::$log->attach(new Kohana_Log_File(APPPATH.'logs/default/debug_log'), array(Log::DEBUG));
    Kohana::$log->attach(new Kohana_Log_File(APPPATH.'logs/default/error_log'), array(Log::ERROR));

    define('_INFO', true);
    define('_DEBUG', true);
}

Language Configuration:
 
$supported_languages = Kohana::$config->load('project.language');
$request_lang = $_REQUEST['lang'];
if ($request_lang !== NULL && $supported_languages[$request_lang])
{
    // attempt to change the language
    Session::instance()->set('current_lang', $supported_languages[$request_lang]);
}
else
{
    $current_lang = Session::instance()->get('current_lang');
    if(empty( $current_lang ) || !in_array($current_lang, $supported_languages))
    {
        // zh_CN by default
        $current_lang = 'zh_CN';
        Session::instance()->set('current_lang', $current_lang);
    }
}
$current_country = Session::instance()->get('current_country');
if(empty( $current_country )){
    // fixed at CN
    Session::instance()->set('current_country', 'CN');
}

Cache configuration:

try {
    if(Kohana::$config->load('project.caching')){
        $memcache = Cache::instance('memcache');
    }
    define('_MEMCACHE', Kohana::$config->load('project.caching'));
} catch (Exception $e) {
    define('_MEMCACHE', false);
    Kohana::$log->add(Log::ERROR,'Fehler beim Memcache initialisieren');
}


Step 4:

Copy /modules/database/config/database.php  to /application/config/database.php

'connection' => array(
    /**
     * The following options are available for MySQL:
     *
     * string   hostname     server hostname, or socket
     * string   database     database name
     * string   username     database username
     * string   password     database password
     * boolean  persistent   use persistent connections?
     *
     * Ports and sockets may be appended to the hostname.
     */
    'hostname'   => 'localhost',
    'database'   => 'patrick',
    'username'   => 'root',
    'password'   => 'pass',
    'persistent' => FALSE,
)

Then you can connect database and operate data.

Kohana View File

Way 1:

$this->response->body(View::factory('site'));
Site means the view file of site.php


Way 2:

$arr = array('xx'=>'12345');
$this->response->body(View::factory('site', $arr));

site.php view code:

<?php echo $xx; ?>


How to use the mysql through kohana

Read data:

Way 1:

$all_kohanas = ORM::factory('user')->where('credit', '>', 20)->find_all();

$result = ORM::factory('user')
                    ->where('id', '=', '1000001')
                    ->find();

$r = $result->as_array();

var_dump($r);

OR

echo $result->id;



Way 2:

$result = Db::select('id', 'username')
                ->from('user')
                ->where('id', '=', '1000002')
                ->execute();

$r = $result->as_array();

var_dump($r);



Insert Data:

Way 1:

$values = array('username' => 'patrick2', 'password' => 'patrick');
$id = Db::insert('user')->values($values)->execute('alternate');


Way 2:
$values = array('username' => 'patrick3', 'password' => 'patrick');
ORM::factory('user')->values($values)->save();


 

Update data:

$values = array('username' => 'patrick3', 'password' => 'patrickwu');
ORM::factory('user', 1)->values($values)->save();


Update the selected items:

ORM::factory('user', 'patrick')->set('password', md5('xxx'))->save();
 

Delete data:

Way 1:

ORM::factory('user', 1)->delete();


Way 2:
ORM::factory('user')->where('id', '=', '1')->find()->delete();

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics