wc之“HelloWorld”

1.介绍一下wc的目录结构,wc也遵循mvc原则

  app相当于controller

  models相当于模块

  plugins插件目录

  staticcss、js文件

  system 引用的类库,由于新手,只能看懂Zend

  views相当于mvc中的视图

2.在浏览器中输入一个url,服务器跳转的过程如下:

  ①例如:话题的URL-->http://dazhuwc.com/?/topic/

  ②apache服务器先找到根目录下index.php文件

if (! file_exists(dirname(__FILE__) . '/system/config/database.php') AND ! file_exists(dirname(__FILE__) . '/system/config/install.lock.php'))
{
    header('Location: ./install/');
    exit;
}

include('system/system.php');

AWS_APP::run();

  ③调用AWS_PAA::run();然后会根据url中topic字段初始化对应的controll,即:models/topic/main.php

public static function run()
    {
        // 全局变量
        global $__controller, $__action, $__default_controller, $__default_action;
        
        self::init();
        
        if (!$app_dir = load_class('core_uri')->set_rewrite()->app_dir)
        {
            $app_dir = ROOT_PATH . 'app/home/';
        }
        
        // 控制器操作进行选择
        if (! $__controller)
        {
            if (isset($_GET['c']))
            {
                $__controller = $_GET['c']; // 有传入控制器字段名
            }
            else
            {
                $__controller = $__default_controller ? $__default_controller : 'main'; // 读取默认控制器字段名
            }
        }
        
        if (! $__action)
        {
            // 动作        
            if (isset($_GET['act']))
            {
                $__action = $_GET['act']; // 有传入动作字段名
            }
            else
            {
                $__action = $__default_action ? $__default_action : 'act'; // 读取默认动作字段名
            }
        }

        // 传入应用目录,返回控制器路径
        
        $handle_controller = self::create_controller($__controller, $app_dir);
        
        $action_method = $__action . '_action';
        
        // 判断
        if (! is_object($handle_controller) || ! method_exists($handle_controller, $action_method))
        {
            HTTP::error_404();
        }
        
        // 判断 ACTION
        if (method_exists($handle_controller, 'get_access_rule'))
        {
            $access_rule = $handle_controller->get_access_rule();
        }
        
        // 判断使用白名单还是黑名单,默认使用黑名单
        if ($access_rule)
        {            
            // 黑名单,黑名单中的检查 'white'白名单,白名单以外的检查(默认是黑名单检查)
            if (isset($access_rule['rule_type']) && $access_rule['rule_type'] == 'white')    //
            {
                if ((! $access_rule['actions']) || (! in_array($__action, $access_rule['actions'])))
                {
                    self::login();
                }
            }
            else if (isset($access_rule['actions']) && in_array($__action, $access_rule['actions']))    // 非白就是黑名单
            {
                self::login();
            }
        
        }
        else
        {
            self::login();
        }
        
        // 执行
        $handle_controller->$action_method();
    }

  ④找到main.php文件后,会定位apache显示views/topic目录下哪个页面(index.tpl.htm或者square.tpl.htm)

public function index_action()
    {
        if ($_GET['id'] or $_GET['title'])
        {
            $this->_topic();
        }
        else
        {
            $this->square_action();
        }
    }

  ⑤因为http://dazhuwc.com/?/topic/此url中没有传id或者title,所以浏览器会显示views/topic/square.tpl.htm视图模板

TPL::output('topic/square');

3.举例说明

  ①先建立一张aws_test数据表,sql如下 :

# 创建语句

CREATE TABLE IF NOT EXISTS `aws_test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL DEFAULT '' COMMENT '测试名',
  `title` text NOT NULL COMMENT '测试title',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

# 插入数据

INSERT INTO `aws_test` (`id`, `name`, `title`) VALUES
(1, '大柱', '大柱加油'),
(2, '大柱2', '大柱2加油');

  ②我决定先创建model.找到models目录,在models下创建文件test.php,我在test.php写了一个查询方法,有分布与排序功能,代码如下:

if (!defined('IN_ANWSION'))
{
    die;
}

class test_class extends AWS_MODEL
{
    /**
     *
     * 测试
     * @param string $where
     * @param int    $limit
     *
     * @return array
     */
    public function get_test_list($where = null, $limit = 10, $order = 'id DESC')
    {
        if ($test_list = $this->fetch_all('test', $where, $order, $limit))
        {
            foreach ($test_list AS $key => $val)
            {
                if (!$val['url_token'])
                {
                    $topic_list[$key]['url_token'] = rawurlencode($val['topic_title']);
                }
            }
        }
            
        return $test_list;
    }
}

  ③再创建controller,找到app,在app下创建文件夹test,然后在test内创建main.php

class main extends AWS_CONTROLLER
{
    public function get_access_rule()
    {
        $rule_action['rule_type'] = "white"; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查

        if ($this->user_info['permission']['visit_topic'] AND $this->user_info['permission']['visit_site'])
        {
            $rule_action['actions'][] = 'index';
        }
        return $rule_action;
    }

    public function index_action()
    {
        $tests = $this->model('test')->get_test_list();
        //为了保持风格一致,css与js必须加上
        TPL::import_css('css/main.css');
        TPL::import_js('js/ajaxupload.js');
        TPL::assign('tests', $tests);
        TPL::output('test/index');
    }
}

  ④接下来创建模板文件,找到views/default目录,在该目录下创建test文件夹,在test文件夹内创建index.php

<?php TPL::output('global/header.tpl.htm'); ?>
<div class="i_bd mainElem i_clear i_allbg">
    <div class="i_left L_sidebar">
            <?php if (is_array($this->tests)) { ?>
                <?php foreach ($this->tests AS $key => $val) { ?>
                <p class="add_ls">
                <?php echo $val['title']; ?><br/>
                <?php echo $key; ?><br/>
                </p>
                <?php } ?>
            <?php } ?>
    </div>
</div>
<?php TPL::output('global/footer.tpl.htm'); ?>

  ⑤大功告成,在浏览器中输入http://dazhuwc.com/?/test即得到下面页面

  

  ⑥晚安。凌晨1点多一点,北京上空又开始飘小雨了。最后说一句:骚年加油吧!

原文地址:https://www.cnblogs.com/fanglove/p/3125508.html