国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

自制迷你小框架

Original 2019-01-13 20:19:46 461
abstract:框架目錄app文件夾是應(yīng)用目錄pig文件夾是核心文件vendor是第三方類庫.htaccess url重寫文件index.php項目入口文件以下是關(guān)鍵文件代碼index.php<?php session_start(); //加載composer自動加載器 require 'vendor/autoload.php'; //加載框架基礎(chǔ)類 require&

框架目錄

QQ圖片20181114103819.png

app文件夾是應(yīng)用目錄

pig文件夾是核心文件

vendor是第三方類庫

.htaccess url重寫文件

index.php項目入口文件

以下是關(guān)鍵文件代碼

index.php

<?php
session_start();
//加載composer自動加載器
require 'vendor/autoload.php';

//加載框架基礎(chǔ)類
require 'pig/Base.php';

//定義項目根目錄
define('ROOT_PATH',__DIR__.'/');

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



(new \pig\Base($config,$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']))->run();

基類控制器

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/10
 * Time: 20:33
 */

namespace pig\core;

use pig\core\View;
class Controller
{
    protected $view = null;

    public function __construct()
    {
        $this->view = new \pig\core\View();
        $this->config();
    }

    public function config()
    {
        //設(shè)置后臺模板目錄
        $this->view->setDirectory(ROOT_PATH.'app/admin/view');
        //給模板目錄起個別名
        $this->view->addFolder('admin',ROOT_PATH.'app/admin/view');
        $this->view->setDirectory(ROOT_PATH.'app/home/view');
        $this->view->addFolder('home',ROOT_PATH.'app/home/view');
    }
}

基類視圖

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/9
 * Time: 20:59
 */

namespace pig\core;

use League\Plates\Engine;

class View extends Engine
{
    //變量容器
    protected $data = [];

    /**
     * View constructor.
     * @param null $directory 模板目錄
     * @param string $fileExtension 模板文件擴(kuò)展
     */
    public function __construct($directory = null, $fileExtension = 'php')
    {
        parent::__construct($directory, $fileExtension);
    }
}

基類模型

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/9
 * Time: 20:51
 */

namespace pig\core;


use Medoo\Medoo;

class Model extends Medoo
{

    public function __construct()
    {
        $config = require ROOT_PATH.'pig/config.php';
        $options = $config['db'];
        parent::__construct($options);
    }
}

配置文件

<?php
//配置文件
return [
  'app'=>[
      'debug'=>true
  ],
  'route'=>[
      'module'=>'admin',
      'controller'=>'Index',
      'action'=>'index'
  ],
    'db'=>[
        'database_type' => 'mysql',
        'database_name' => 'frame',
        'server' => '127.0.0.1',
        'username' => 'root',
        'password' => 'root'
    ]

];

路由文件

<?php
namespace pig;
class Route
{
    //保存路由配置信息
    protected $route = [];
    //保存url路徑信息
    protected $pathinfo = [];
    //保存url參數(shù)
    protected $param = [];

    public function __construct($route)
    {
        $this->route = $route;
    }
    //解析路由
    //http://frame.io/[index.php]/admin/Index/index
    public function parse($url)
    {
        if(strpos($url,'.php')){
            $queryStr = strchr($url,'.php');
            $queryStr = trim(substr($queryStr,4),'/');
        }else{
            $domainLen = strlen($_SERVER['SERVER_NAME']);
            $queryStr = strchr($url,$_SERVER['SERVER_NAME']);
            $queryStr = trim(substr($queryStr,$domainLen),'/');
        }
        $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];
                //處理參數(shù)
                $paramArr = array_slice($queryArr,3);
                for($i=0;$i<count($paramArr);$i+=2){
                    if(!empty($paramArr[$i+1])){
                        $this->param[$paramArr[$i]] = $paramArr[$i+1];
                    }
                }
        }
        return $this;
    }
    //路由分發(fā)
    public function dispatch()
    {
        $module = $this->getModule();
        $controller = $this->getController();
        $action = $this->getAction();
        if(!method_exists($controller,$action)){
            header('Location:/');
        }

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

    public function getModule()
    {
        return $this->pathinfo['module'];
    }
    public function getParam()
    {
        return $this->param;
    }
    public function getController()
    {
        $module = $this->getModule();
        if(empty($this->pathinfo['controller'])){
            $this->pathinfo['controller'] = $this->route['controller'];
        }
        $controller = 'app\\'.$module.'\controller\\'.ucfirst($this->pathinfo['controller']);
        return $controller;
    }
    public function getAction()
    {
        if(empty($this->pathinfo['action'])){
            $this->pathinfo['action'] = $this->route['action'];
        }
        return $this->pathinfo['action'];
    }
}
//$url = 'http://frame.io/admin/Index/index/name/peter/age/30';
//$config = require __DIR__.'/config.php';
//$route = new Route($config['route']);
//$route->parse($url);
//require __DIR__.'/../app/admin/controller/Index.php';
//$route->dispatch();

框架引導(dǎo)文件

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/8
 * Time: 20:59
 */

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{
            ini_set('display_errors','Off');
            ini_set('log_errors','On');
        }
    }
    public function loader($class)
    {
        $path = str_replace('\\','/',$class).'.php';
        if(!file_exists($path)){
            header('Location:/');
        }
        require $path;
    }
    public function run()
    {
        //調(diào)試模式
        $this->setDebug();
        //自動加載
        spl_autoload_register([$this,'loader']);
        //請求分發(fā)
        (new Route($this->config['route']))->parse($this->queryStr)->dispatch();
    }
}

應(yīng)用實現(xiàn)的主控制器文件

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/8
 * Time: 15:54
 */

namespace app\admin\controller;


use app\model\User;
use pig\core\Controller;

class Index extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {

        $model = new User();
        $rows = $model->select($model->getTable(),['id','name','dept','art','email','create_time'],[
            'dept[~]'=>isset($_POST['dept'])?$_POST['dept']:null
        ]);
        echo $this->view->render('admin::index/index',[
            'title'=>'武林高手排行榜',
            'rows'=>$rows,
            'searchUrl'=>'/admin/index/index',
            'loginUrl'=>'/admin/index/login',
            'logoutUrl'=>'/admin/index/logout',
            'insertPage'=>'/admin/index/insert',
            'editUrl'=>'/admin/index/edit',
            'delUrl'=>'/admin/index/del'
        ]);
    }
    public function login()
    {
        $model = new User();
        $res = $model->get('admin',['id','name'],[
            "AND" => [
                "password" => sha1($_POST['password']),
                "email" => $_POST['email']
            ]
        ]);

        if($res){
            $_SESSION['name'] = $res['name'];
            $_SESSION['id'] = $res['id'];
            echo '<script>alert("登錄成功");location.href="/";</script>';

        }else{
            echo '<script>alert("郵箱或密碼錯誤");location.href="/";</script>';
        }
    }

    public function logout()
    {
        session_destroy();
        echo '<script>alert("退出成功");location.href="/";</script>';
    }

    public function insert()
    {
        echo $this->view->render('admin::index/insert',['doInsertUrl'=>'/admin/index/doInsert']);
    }
    public function doInsert()
    {
        $model = new User();
        $data = $_POST;
        $data['create_time'] = time();
        $res = $model->insert($model->getTable(),$data);
        if($res){
            echo '<script>alert("插入成功");location.href="/";</script>';
        }else{
            echo '<script>alert("插入失敗");</script>';
        }
    }
    public function edit($id)
    {
        $model = new User();
        $res = $model->get($model->getTable(),'*',['id'=>$id]);
        echo $this->view->render('admin::index/edit',['res'=>$res,'doEditUrl'=>'/admin/index/doEdit']);
    }

    public function doEdit($id)
    {
        $model = new User();
        $res = $model->update($model->getTable(),$_POST,['id'=>$id]);
        if($res){
            echo '<script>alert("編輯成功");location.href="/";</script>';
        }else{
            echo '<script>alert("編輯失敗");</script>';
        }
    }

    public function del($id)
    {
        $model = new User();
        $res = $model->delete($model->getTable(),['id'=>$id]);
        if($res){
            echo '<script>alert("刪除成功");location.href="/";</script>';
        }else{
            echo '<script>alert("刪除失敗");</script>';
        }
    }
}

項目效果圖

QQ圖片20181114103819.png

Correcting teacher:天蓬老師Correction time:2019-01-14 10:06:44
Teacher's summary:復(fù)制的時候,居然連注釋掉的調(diào)試信息也一并復(fù)制過來, 難道就不能自己動手寫寫嗎? 如果你總是這樣的話, 學(xué)習(xí)進(jìn)度和效率會很低的, 下次希望你能自己親自動手寫寫, 畫一下流程圖, 把老師的這個框架案例的畫一下, 理解其中包括的知識點

Release Notes

Popular Entries