Monday, March 28, 2011

MVC Frameworks Document (1)

./controllers/api_welcomes_controller.php
/**
 * Example Controller
 * 網址範例 http://localhost/api.php?c=api_welcomes&a=index&p[]=p1&p[]=p2
 * 參數 p 是一個矩陣, 可以帶到 action 使用
 * 目前 index 這個 actin 有兩個引數 ($p1 和 $p2)
 * 所以要帶兩個參數 p (p[]=p1&p[]=p2) 來滿足該 action 的引數需求
 */
class STOCK_ApiWelcomesController extends STOCK_AppController
{
  public function index($p1, $p2)
  {
    // using argument example
    d($p1);
    d($p2);
    
    // load model example
    $this->load->model('example');
    $this->m_example->info();
    $this->load->model('example2');
    $this->m_example2->info();
    
    // load config example
    $config = $this->load->config('auth_exception');
    
    // set view data
    $this->view->set('abc.def', 123456);
    $this->view->set('abc.f', 456);
    $this->view->set('abc.def.xyz', 6);
    // render a element
    $this->view->render_element(
      'example',    // element name
      array('test' => 'this is a element test (1).')
    );
    // set veiw layout name
    $this->view->set_layout('');
    // render a layout
    $this->view->render();
  }
}

./models/example.php
class STOCK_Example extends STOCK_AppModel
{
  public function __construct()
  {
    parent::__construct();
  }
  
  public function info()
  {
    $this->load_database();
    d($this->db);
    $this->load_memcache();
    d($this->mc);
    // load model is also avaliable!
    $this->load_model('user');
    d($this->m_user);
  }
}

./views/api_welcomes/index.php
<p>
---------- This is work! ---------- line 2 ----------
</p>
<?php echo d($abc); ?>
<?php
$this->render_element(
  'example',
  array('test' => 'this is a element test (2).')
);
?>

./views/elements/example.php
<div><?php echo $test; ?></div>

在任何地方插入下面的程式, 可以完成事件的處理 (Event Handle), event 的程式放在 ./events/ 下, 檔名即為 event name
include(event('example'));

No comments: