21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
//项目路径
defined('APP_PATH') or define('APP_PATH', getcwd() . '/app');
define('CONTROLLERS_PATH', APP_PATH . '/controllers');
define('MODELS_PATH', APP_PATH . '/models');
define('VIEWS_PATH', APP_PATH . '/views');
//当前访问的 模块、控制器、方法
$m = isset($_GET['m']) ? $_GET['m'] : 'home';
$c = isset($_GET['c']) ? $_GET['c'] : 'index';
$a = isset($_GET['a']) ? $_GET['a'] : 'index';
define('MODULE_NAME', $m);
define('CONTROLLER_NAME', $c);
define('ACTION_NAME', $a);
//自动加载
function autoload($class_name)
{
$type = substr($class_name, -10) == 'Controller' ? 'controller' : 'model';
if ($type == 'controller') {
//模块名
$module = substr($class_name, 0, -10) == 'Common' ? '' : "/" . MODULE_NAME;
//控制器名
$controller_name = '/' . strtolower(substr($class_name, 0, -10)) . '_controller.php';
//控制器完整路径
$file = CONTROLLERS_PATH . $module . $controller_name;
}
if ($type == 'model') {
$name = strtolower(substr($class_name, 0, -5));
$file = MODELS_PATH . "/{$name}_model.php";
}
if (file_exists($file)) {
require $file;
}
}
//注册自动加载
spl_autoload_register("autoload");
$controller_name = ucfirst($c) . 'Controller';
$controller = new $controller_name;
$controller->$a();
/*
* 实例化公共模型
*/
function M($table)
{
return new CommonModel($table);
}
/*
* 实例化自定义的模型
*/
function D($table)
{
$model = ucfirst($table) . 'Model';
return new $model($table);
}
/**
* 读取配置文件
* @param $key
* @return mixed
*/
function C($key)
{
$config = require getcwd() . '/config/common.php';
return $config["$key"];
}
function dump($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}