iMooc上的MVC教程练习。
MVC是PHP基础和进阶的分界点吧应该说是
一、准备
工欲善其事~
个人环境:windows10+wamp2.5+ZendStudio12;
项目名称:MVCEasyFirst;
文件命名:testController.class.php
文件名 控制器 类文件类型
代码标准:PSR-2;
二、搭建
Controller
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">namespace MVCEasyFirst\Controller\test; </span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">use</span><span style="color: #000000;"> MVCEasyFirst\Model\test\testModel; </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">use</span><span style="color: #000000;"> MVCEasyFirst\View\test\testView; </span><span style="color: #008080;"> 6</span> <span style="color: #008000;">//</span><span style="color: #008000;">控制器是唯一需要和用户进行连接的地方 </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//控制器“承上启下” </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//控制器负责:1、接收指令;2、调用模型并接收模型处理好的数据;3、调用视图(传递数据给视图)</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> testController </span><span style="color: #008080;">10</span> <span style="color: #000000;">{ </span><span style="color: #008080;">11</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> show() </span><span style="color: #008080;">12</span> <span style="color: #000000;"> { </span><span style="color: #008080;">13</span> <span style="color: #800080;">$testModel</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> testModel(); </span><span style="color: #008080;">14</span> <span style="color: #800080;">$data</span>=<span style="color: #800080;">$testModel</span>-><span style="color: #000000;">get(); </span><span style="color: #008080;">15</span> <span style="color: #800080;">$testView</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> testView(); </span><span style="color: #008080;">16</span> <span style="color: #800080;">$testView</span>->display(<span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #008080;">17</span> <span style="color: #000000;"> } </span><span style="color: #008080;">18</span> }
Model
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">namespace MVCEasyFirst\Model\test; </span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #008000;">//</span><span style="color: #008000;">模型负责:1、接收指令;2、处理并返回数据 </span><span style="color: #008080;"> 5</span> <span style="color: #008000;">//双向,但仅与控制器相连接</span> <span style="color: #008080;"> 6</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> testModel </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get() </span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> { </span><span style="color: #008080;">10</span> <span style="color: #0000ff;">return</span> "Hello world!"<span style="color: #000000;">; </span><span style="color: #008080;">11</span> <span style="color: #000000;"> } </span><span style="color: #008080;">12</span> }
View
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">namespace MVCEasyFirst\View\test; </span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #008000;">//</span><span style="color: #008000;">视图负责:1、接受指令;2、显示相关结果给浏览器/用户; </span><span style="color: #008080;"> 5</span> <span style="color: #008000;">//单向通路,C->V->User</span> <span style="color: #008080;"> 6</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> testView </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> display(<span style="color: #800080;">$data</span><span style="color: #000000;">) </span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> { </span><span style="color: #008080;">10</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$data</span><span style="color: #000000;">; </span><span style="color: #008080;">11</span> <span style="color: #000000;"> } </span><span style="color: #008080;">12</span> }
三、调用
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">use</span><span style="color: #000000;"> MVCEasyFirst\Controller\test\testController; </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">require_once</span> 'testController.class.php'<span style="color: #000000;">; </span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">require_once</span> 'testModel.class.php'<span style="color: #000000;">; </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">require_once</span> 'testView.class.php'<span style="color: #000000;">; </span><span style="color: #008080;"> 6</span> <span style="color: #008000;">//</span><span style="color: #008000;">MVC大量依赖引入,推荐require,和我之前想的不太一样 </span><span style="color: #008080;"> 7</span> <span style="color: #008000;">//其实这个问题我个人觉得还是要分情况来说的,看是什么需求,需要运行(警告)还是发现问题所在(错误)</span> <span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #800080;">$testController</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> testController(); </span><span style="color: #008080;">10</span> <span style="color: #800080;">$testController</span>->show();
四、小结
简单的小例子,直观的了解MVC模型!!!
注意区分清楚各个模块的“职责”!
再有就是规范开发的情况:
五、入口程序与系统目录的规范化
把 MVC应用到生产中;
单一入口机制:在一个web应用中,所有的请求都指向一个脚本文件,比如alksdjfklajfklj.php,也就是说,所有对程序的访问都是必须通过这个入口。
优点:得以实现MVC;对什么安全什么的也似乎比较方便控制。
系统目录的规范化:
我就打算先这么干,至于以后那再说。
六、简化M\V\C调用
前面说过,MVC大量以来引用/调用,所以对于调用,不如写一个函数来集成/封装
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">namespace MVC\func; </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">//</span><span style="color: #008000;">一些框架中简化用到的函数</span> <span style="color: #008080;"> 4</span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">function</span> C(<span style="color: #800080;">$name</span>,<span style="color: #800080;">$method</span><span style="color: #000000;">) </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">require_once</span> '/libs/Controller/'.<span style="color: #800080;">$name</span>.'Controller.class.php'<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">eval('$obj=new '.$name.'Controller();$obj->'.$method.'();');</span> <span style="color: #008080;"> 9</span> <span style="color: #800080;">$controller</span>=<span style="color: #800080;">$name</span>.'Controller'<span style="color: #000000;">; </span><span style="color: #008080;">10</span> <span style="color: #800080;">$obj</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$controller</span><span style="color: #000000;">(); </span><span style="color: #008080;">11</span> <span style="color: #800080;">$obj</span>-><span style="color: #800080;">$method</span><span style="color: #000000;">(); </span><span style="color: #008080;">12</span> <span style="color: #000000;">} </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #0000ff;">function</span> M(<span style="color: #800080;">$name</span><span style="color: #000000;">) </span><span style="color: #008080;">15</span> <span style="color: #000000;">{ </span><span style="color: #008080;">16</span> <span style="color: #0000ff;">require_once</span> 'libs/Model/'.<span style="color: #800080;">$name</span>.'Model.class.php'<span style="color: #000000;">; </span><span style="color: #008080;">17</span> <span style="color: #800080;">$model</span>=<span style="color: #800080;">$name</span>.'Model'<span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #800080;">$obj</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$model</span><span style="color: #000000;">(); </span><span style="color: #008080;">19</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj</span><span style="color: #000000;">; </span><span style="color: #008080;">20</span> <span style="color: #000000;">} </span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #0000ff;">function</span> V(<span style="color: #800080;">$name</span><span style="color: #000000;">) </span><span style="color: #008080;">23</span> <span style="color: #000000;">{ </span><span style="color: #008080;">24</span> <span style="color: #0000ff;">require_once</span> 'libs/View/'.<span style="color: #800080;">$name</span>.'View.class.php'<span style="color: #000000;">; </span><span style="color: #008080;">25</span> <span style="color: #800080;">$view</span>=<span style="color: #800080;">$name</span>.'View'<span style="color: #000000;">; </span><span style="color: #008080;">26</span> <span style="color: #800080;">$obj</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$view</span><span style="color: #000000;">(); </span><span style="color: #008080;">27</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj</span><span style="color: #000000;">; </span><span style="color: #008080;">28</span> }
其中,eval函数我也是第一次用,视频中讲不安全,就换成下面这些就好。
<span style="color: #008080;">1</span> <span style="color: #000000;">php </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">require_once</span> 'func.php'<span style="color: #000000;">; </span><span style="color: #008080;">3</span> <span style="color: #008080;">4</span> \MVC\func\C('test', 'show');
<span style="color: #008080;">1</span> <span style="color: #000000;">php </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> testController </span><span style="color: #008080;">3</span> <span style="color: #000000;">{ </span><span style="color: #008080;">4</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> show() </span><span style="color: #008080;">5</span> <span style="color: #000000;"> { </span><span style="color: #008080;">6</span> <span style="color: #0000ff;">echo</span> "hello"<span style="color: #000000;">; </span><span style="color: #008080;">7</span> <span style="color: #000000;"> } </span><span style="color: #008080;">8</span> }
--------4/28更新---------------
我的环境问题或者其他什么,动态实例化类不可行,暂时放弃了,有哪个大师知道为什么,烦请告知啊~
环境:zend12+PHP5.5
这部分暂时这样,下一部分是smarty