PHP-MVC框架搭建之入口文件

MVC框架目录结构

index.php作为单一入口,所有请求只能访问index.php,并且要携带平台信息和控制器。

<?php 

	//增加入口标记
 	define('ACCESS',TRUE);

 	//定义项目根目录
 	define('ROOT_PATH',str_replace('\\', '/', dirname(__DIR__)).'/');
 	//echo ROOT_PATH;die;

 	//加载初始化文件
 	require ROOT_PATH . 'core/app.php';


 	//激活初始化文件
 	\core\App::start();

 ?>

  我们通过index.php加载初始化类文件app.php,然后在初始化类文件中定义路径常量,设置系统设置,加载配置文件,URL解析,自动加载和分发控制,实现对框架的基本访问。

<?php 
//增加命名空间
namespace core;

//判断是否合法访问,是否是从index.php文件访问过来的
if(!ACCESS){
	header('location:../public/index.php');
	exit;
}


//初始化类
class App{
	//入口方法
	public static function start(){
		
		//加载路径常量
		self::set_path();

		//配置文件
		self::set_config();

		//错误控制
		self::set_error();

		//解析URL
		self::set_URL();

		//自动加载
		self::set_autolode();

		//分发
		self::set_dispatch();
	}

	//定义路径常量
	private static function set_path(){
		define('CORE_PATH',ROOT_PATH . 'core/');
		define('APP_PATH',ROOT_PATH . 'app/');
		define('CONFIG_PATH',ROOT_PATH . 'config/');
		define('VENDOR_PATH',ROOT_PATH . 'vendor/');

	}

	//错误方法控制
	private static function set_error(){
		global $config;
		//var_dump($config);die;
		//E_ALL为系统常量,显示全部错误
		@ini_set('error_reporting', $config['error']['error_reporting']); 
		//1 显示错误信息  0 不显示
		@ini_set('display_errors', $config['error']['display_errors']); 
	}

	//配置文件
	private static function set_config(){
		//设置全局变量$config 保存配置信息
		global $config;
		//包含配置文件
		$config = include '../config/config.php';
	}

	//解析URL
	private static function set_URL(){
		//p平台信息 c控制器 a访问方法
		$p = $_REQUEST['p'] ?$_REQUEST['p'] :'home'; //默认访问home
		$c = $_REQUEST['c'] ?$_REQUEST['c'] :'Index'; //默认访问Index控制器
		$a = $_REQUEST['a'] ?$_REQUEST['a'] :'index'; //默认访问index方法

		define('p', $p);
		define('c', $c);
		define('a', $a);
	}

	//自动加载
	private static function set_autoload_function($class){
		//取出类名
		$class = basename($class);

		//判定核心文件是否存在
		$core_file = CORE_PATH . $class . '.php';
		if(file_exists($core_file)) include $core_file;

		//判定controller文件是否存在
		$controller_file =  APP_PATH.  p . '/controller/' .$class . '.php';
		if(file_exists($controller_file)) include $controller_file;
		//echo $controller_file;
		

		//判定model文件是否存在
		$model_file = APP_PATH. p . '/model/' .$class . '.php';
		if(file_exists($model_file)) include $model_file;

		//判定model文件是否存在
		$vendor_file = VENDOR_PATH .$class . '.php';
		if(file_exists($vendor_file)) include $vendor_file;
	}

	//注册自动加载
	private static function set_autolode(){
		//利用spl_autoload_registers实现注册
		spl_autoload_register(array(__CLASS__,'set_autoload_function'));
	}

	//分发控制器
	private static function set_dispatch(){
		//访问路径 平台信息/控制器/方法
		$p = p;
		$c = c;
		$a = a;

		//组合
		$controller = "\\{$p}\\controller\\{$c}Controller";
		//echo $controller;
		$c = new $controller();
		//实现分发
		$c->$a();
	}

}



 ?>

  配置文件config.php

<?php 
	//配置文件
	return array(
		//数据库配置
		'database' => array(
			'type' => 'mysql',
			'host' => 'localhost',
			'port' => '3306',
			'user' => 'root',
			'password' => '123456',
			'dbname' => 'test',
			'charset' => 'utf8'

		),

		//错误控制
		'error' => array(
			'error_reporting' => 'E_ALL',
			'display_errors' => 1
		)

		//其他配置

	)
 ?>

 

 

  为了方便测试,我们设置默认访问文件,\app\home\controller\IndexController.php。

<?php 

//定义命名空间
namespace home\controller;

class IndexController{
  	//默认方法
  	public function index(){
  		echo '欢迎使用MVC';
  	}
}

 ?>

  默认访问框架。

 

这样我们就实现了MVC框架开发的入口文件的开发。接下来我还会继续介绍其他文件的基本开发。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值