亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

創(chuàng)建一個(gè)mvc應(yīng)用目錄架構(gòu)并創(chuàng)建入口文件index.php

Original 2019-05-13 04:24:02 261
abstrakt:<?php require 'vendor/autoload.php'; require 'pig/Base.php'; define('ROOT_PATH',__DIR__.'/'); $config=require 'pig/config.php'; $queryStr=$_S

FastStoneEditor1.jpg

<?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 打開(kāi)執(zhí)行后實(shí)例化框架基礎(chǔ)類的調(diào)試狀態(tài),自動(dòng)加載類文件如:當(dāng)用戶提供:php.edu/demo/index/a 后框架自動(dòng)加載檢測(cè)準(zhǔn)備new的類生成絕對(duì)路徑.

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


<?php
/**
 *  * 框架基礎(chǔ)類
 * 1. 調(diào)試模式
 * 2. 自動(dòng)加載
 * 3. 啟動(dòng)框架
 */

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.'<br>';
        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
/**
*路由、分發(fā)類
 */
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<count($arr);$i +=2){
                    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";
    }
}

20190513042255.jpg

Korrigierender Lehrer:天蓬老師Korrekturzeit:2019-05-13 09:17:42
Zusammenfassung des Lehrers:通過(guò)這個(gè)小框架 , 相信你對(duì)一個(gè)php框架的運(yùn)行的基本流程有了一個(gè)大致的了解... 一個(gè)真實(shí)商用的php框架, 它的復(fù)雜度遠(yuǎn)高于這個(gè)教學(xué)框架, 但是底層的原理與架構(gòu), 與這個(gè)并無(wú)本質(zhì)區(qū)別.. 掌握 這個(gè)小框架的編程思想和思維模式, 對(duì)于你以后學(xué)習(xí)成熟的框架開(kāi)發(fā), 如laravel, thinkphp,ci, yii等, 非常有好處的, 加油...

Versionshinweise

Beliebte Eintr?ge