返回 MVC应用目录...... 登陆

MVC应用目录架构,并创建一个入口文件index.php

肖凌 2019-10-20 23:10:41 613

QQ截图20191020230053.png

//入口文件 index.php

<?php

     require 'vendor/autoload.php';

    define('PATH_NAME',__DIR__);

    require 'cfg/base.php';

    use cfg\base;

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

    $qStr = $_SERVER['QUERY_STRING'];

    $base = new base($config,$qStr);

    $base->setDebug();

    echo $base->run();

//

<?php 

//继承视图类     view.php

    namespace cfg\exd;

    use League\Plates\Engine;

    class view extends Engine{

        public function __construct(){

                parent::__construct($directory = null, $fileExtension = 'php');

        }

    }

<?php

//继承数据库类 model.php

         namespace cfg\exd;

        use Medoo\Medoo;

        class model extends Medoo{

                public function __construct(){

                        $option = require 'cfg/config.php';

                        parent::__construct($option['Db']);

                 }

         }


<?php

//构造控制器类 controller.php

         namespace cfg\exd;

        use cfg\exd\view;

        class controller{

                protected $view;

                protected $data=[];

                 //构造控制器对象同时构造视图对象

                 public function __construct(){

                         //构造视图

                         $this->view = new view;

                         //设置视图目录

                         $this->setDirectory();

                         //设置视图别名

                         $this->addFolder();

                }

                 //重载视图目录

                 public function setDirectory(){

                        $this->view->setDirectory(PATH_NAME.'/app/admin/view');

                }

                 //重载视图别名目录

                 public function addFolder(){

                        $this->view->addFolder('admin',PATH_NAME.'/app/admin/view');

                }

                 //重载视图渲染

                 public function render($path){

                        return $this->view->render($path,$this->data);

                }

        }


//控制器类 Index.php

<?php

namespace app\admin\controller;

use app\model\User;

use cfg\exd\controller;

class Index extends controller {

        public function index(){

                return 'my name is whyan20191020';

        }

        public function user(){

                         //声名模型对象

                         $users = new User;

                         //返回数据数组

                         $result = $users->select('m_users','*',['user_id[>]'=>3]);

                         //模板变量赋值

                         $this->data['title'] = 'welcome';

                        $this->data['result'] = $result;

                        //渲染模板

                         return $this->render('index/user');

                 }

}

//渲染user.php

<!DOCTYPE html>

<html>

         <head>

                  <title><?=$title?></title>

         </head>

         <body>

                 <p>

                         <?php

                         echo '<pre>'.var_export($result,true).'<br>';

                         ?>

                 </p>

         </body>

</html>


最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网