php好的mvc中index方法,创建一个mvc应用目录架构并创建入口文件index.php

摘要:<?php

require 'vendor/autoload.php';

require 'pig/Base.php';

define('ROOT_PATH',__DIR__.'/');

$config=require 'pig/config.php';

$queryStr=$_S

6439cc925a4f5b7600e340f552aae664.png<?php

require 'vendor/autoload.php';

require 'pig/Base.php';

define('ROOT_PATH',__DIR__.'/');

$config=require 'pig/config.php';

$queryStr=$_SERVER['REQUEST_URI'];

(new \pig\Base($config,$queryStr))->run();

框架入口文件 index.php 打开执行后实例化框架基础类的调试状态,自动加载类文件如:当用户提供:php.edu/demo/index/a 后框架自动加载检测准备new的类生成绝对路径.

D:\myphp_www\PHPTutorial\WWW\php.edu/pig/Route.php

D:\myphp_www\PHPTutorial\WWW\php.edu/app/admin/controller/Index.php

/**

*  * 框架基础类

* 1. 调试模式

* 2. 自动加载

* 3. 启动框架

*/

namespace pig;

class Base

{

protected  $config=[];

protected  $queryStr='';

public function __construct($config,$queryStr)

{

$this->config=$config;

$this->queryStr=$queryStr;

}

public function setDebug()

{

if($this->config['app']['debug']){

error_reporting(E_ALL);

ini_set('display_errors','On');

}else{

error_reporting(E_ALL);

ini_set('display_errors','off');

ini_set('log_errors','On');

}

}

public function loader($class)

{

$path = ROOT_PATH.str_replace('\\','/',$class).'.php';

echo $path.'
';

if (!file_exists($path)){

header('Location:/');

}

require $path;

}

public function run()

{

$this->setDebug();

spl_autoload_register([$this,'loader']);

echo(new Route($this->config['route']))->parse($this->queryStr)->dispatch();

}

}<?php

/**

*路由、分发类

*/

namespace pig;

class Route

{

//    protected $route = [];

public $route = [];

//    protected $pathInfo=[];

public $pathInfo=[];

//    protected $params=[];

public $params=[];

public function __construct($route)

{

$this->route=$route;

}

public function parse($queryStr='')

{

$queryStr=trim(strtolower($queryStr),'/');

$queryArr=explode('/',$queryStr);

$queryArr=array_filter($queryArr);

switch (count($queryArr))

{

case 0:

$this->pathInfo =$this->route;

break;

case 1:

$this->pathInfo['module']=$queryArr[0];

break;

case 2:

$this->pathInfo['module']=$queryArr[0];

$this->pathInfo['controller']=$queryArr[1];

break;

case 3:

$this->pathInfo['module']=$queryArr[0];

$this->pathInfo['controller']=$queryArr[1];

$this->pathInfo['action']=$queryArr[2];

break;

default:

$this->pathInfo['module']=$queryArr[0];

$this->pathInfo['controller']=$queryArr[1];

$this->pathInfo['action']=$queryArr[2];

$arr=array_slice($queryArr,3);

for ($i=0;$i

if (isset($arr[$i+1])){

$this->params[$arr[$i]]=$arr[$i+1];

}

}

break;

}

return $this;

}

public function dispatch()

{

$module=$this->pathInfo['module'];

$controller='\app\\'.$module.'\controller\\'.$this->pathInfo['controller'];

$action = $this->pathInfo['action'];

//        new $controller;

if(!method_exists($controller,$action)){

$action=$this->route['action'];

header('Location:/');

}

return call_user_func_array([new $controller,$action],$this->params);

}

}<?php

/**

* Created by PhpStorm.

* User: 普通用户

* Date: 2019/5/13

*

*/

namespace app\demo\controller;

class index

{

public function a()

{

echo "demo->index->a";

}

}

f8f7113e49356759fab487cfcbe994f6.png

批改老师:天蓬老师批改时间:2019-05-13 09:17:42

老师总结:通过这个小框架 , 相信你对一个php框架的运行的基本流程有了一个大致的了解...

一个真实商用的php框架, 它的复杂度远高于这个教学框架, 但是底层的原理与架构, 与这个并无本质区别..

掌握 这个小框架的编程思想和思维模式, 对于你以后学习成熟的框架开发, 如laravel, thinkphp,ci, yii等, 非常有好处的, 加油...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值