基于Medoo的簡單的mvc框架:Medoo-MVC

jopen 10年前發布 | 53K 次閱讀 Medoo-MVC PHP開發

Medoo-MVC是一個基于Medoo的簡單的php框架,如果之前使用過Medoo,只需幾分鐘學習即可開始開發,如果沒接觸過Medoo,可能會需要十多分鐘學習一下。

Medoo-MVC快速開始:

如果還不了解Medoo,可以參考官方文檔:http://medoo.in/,這里主要介紹Medoo-MVC中的新內容。

1.config

數據庫連接配置文件單獨存放在”/config/db.config.php”,默認使用mysql數據庫,更改username,password,dbname后可開始其他操作。

2.Controller

通過Controller和Controller中的方法來實現功能,Controller都繼承自medoo類,這樣可以直接調用medoo的方法:

<?php

class indexController extends medoo
{
    function index()
    {
        $datas['title'] = 'Medoo-MVC';

        ...

        $this->display( $datas );
    }

}

3.View

在Controller的方法中給變量賦值,然后通過display的參數傳入模板顯示:

如在indexController中的index方法為數組$datas的title元素賦值,通過display( $datas )傳入index.html模板顯示;

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title><?php echo $title; ?></title>
...

因為使用了extract處理傳入參數,在模板中只需通過數組的key作為變量名即可調用變量的值,$title即是$datas['title']。

4.Model

如果使用model,建議繼承medoo類,這樣就可以調用medoo的數據操作方法,添加model后在同名的Controller中可以直接實例化model類:

index.model.php
<?php

class index extends medoo
{
    public function hello()
    {
        $datas[0] = 'This is function hello() of model index.';
        $datas[1] = 'datas from database.';
        return $datas;
    }
}
index.class.php
<?php

class indexController extends medoo
{
    function index()
    {
        ...

        $hello = new index();
        $datas['hello'] = $hello->hello();

        ...

        $this->display( $datas );
    }

}
index.html
...
<p>
    <?php if(isset($hello) && is_array($hello)):?>
        <?php foreach ($hello as $key => $value): ?>
            <p><?=$value; ?></p>
        <?php endforeach; ?>
    <?php endif; ?>
</p>
...
output:This is function hello() of model index.
datas from database.

5.全局函數

需要隨處調用的自己添加的函數可寫到”/lib/function/app.php”。

6.擴展類

自己添加的擴展類放到”/lib/class/”,之后就可在程序中實例化使用該擴展類:

 a.class.php
<?php
class a
{
    public function hi()
    {
        return 'This is function hi() of lib class a.';
    }
}
 index.class.php
<?php

class indexController extends medoo
{
    function index()
    {
        ...

        $lib_class = new a();
        $datas['hi'] = $lib_class->hi();

        $this->display( $datas );
    }

}

引入的擴展類可以在控制器中調用。

Medoo-MVC下載地址:https://github.com/XuHaixiao/Medoo-MVC

Medoo-MVC開發的項目“shop72hour”:http://www.xuhaixiao.com/shop72hour/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!