Home Backend Development PHP Tutorial 【PHP】MVC框架为何这么难! 4年后终于明白了,被ThinkPHP害得!

【PHP】MVC框架为何这么难! 4年后终于明白了,被ThinkPHP害得!

Jun 13, 2016 am 11:59 AM
action controller mvc nbsp thinkphp

【PHP】MVC框架为什么这么难!!! 4年后终于明白了,被ThinkPHP害得!!

本帖最后由 default7 于 2014-05-15 18:39:05 编辑
这个帖子送给那些被ThinkPHP命名搞得晕头转向的PHP程序员~(by default7#zbphp.com)


4年前我的几个帖子

帖子1 《跟别人争论PHP中MVC的问题》 http://bbs.csdn.net/topics/350216861
发表于: 2010-12-10 22:38:53 
其实这个帖子当时是使用ThinkPHP,但是运行极为缓慢,且网址怎么都对不上,最后放弃了使用MVC框架!

帖子2 《貌似突然理解了MVC模型》http://bbs.csdn.net/topics/390332774
发表于: 2012-12-29 16:47:22 
这个帖子里面对MVC的理解其错误的,那种方法是工厂模式,并非MVC模式。



PHP MVC真心折磨人啊!!!~

MVC 是Model View Controller的缩写,是一种程序设计方式。可我虽然工作已经有五六年,一直做PHP程序员,但是始终没用过MVC框架更一直被MVC搞的晕头转向。国内MVC框架基本上都是ThinkPHP,很多次找工作,因为不会MVC,因为不用ThinkPHP框架,所以一直碰壁,越来越迷惑 怀疑 不解 抑郁。

这一次再次硬着头皮去理解掌握的心态,重新阅读ThinkPHP框架的官网资料,不过还是很遗憾,网址对应这里仍旧没看懂,越看越晕。不是说MVC吗,为什么没有Controller类? 为什么会出现一个Action类?“控制器”怎么不见了,但是却有一个“Action”? 为什么有“网址参数a”和“参数m”?“参数a”是不是指代“Action类”?“参数m”是不是指代“Model类”??


ThinkPHP 为什么会这么难
<br /><?php<br />//文件地址:D:\zbphp.com\www\Lib\Action\MemberAction.class.php<br />class MemberAction extends Action {<br /><br />    public function index(){<br />        $this->display();<br />    }<br /><br />    public function login(){<br />        $this->display();<br />    }<br /><br />    public function register(){<br />        $this->display();<br />    }<br />}<br />
Copy after login

每次看到ThinkPHP引以为傲的路由网址,就觉得晕!index.php?a=&m=,看到这样的网址,心里就会想“参数a的值”应该是指代“Action类”吧,那数“参数m”应该是指代MVC中的Model吧(模型)?

但是又觉得哪里不对。
Action是动作的意思,MVC是Model View Controller,那么为什么没有看到Controller类?后来仔细看,原来Action在ThinkPHP中就是控制器(但为什么要命名为Action??为什么不直接命名为Controller??)

再来说参数m,一开始理解就是Model类吧。但是更觉得奇怪,比如访问 index.php?a=index&m=content ,那按照这样的思路访问的就是 class IndexAction 下面的 content() 方法,不过很遗憾,错了。他访问的是 class ContentAction 下面的 index()方法。。。

到了这里,你就会明白了为什么ThinkPHP框架会这么难会这么复杂,因为他用了极容易混淆的方式来命名关键部分

害人不浅的ThinkPHP

写到这里我真的忍不住想说ThinkPHP害人不浅,命名上弄出了
1) 网址参数a和类中的Action;
2) 网址参数m 和类中的Model;
3) 是MVC框架但却用Action类来表示Controller;

由于这三点让绝大多数想弄明白MVC的PHP程序员每次看都是一头雾水!

如果ThinkPHP 这样写,大部分人会立即明白MVC架构
<br /><?php<br />//文件 D:\zbphp.com\www\Lib\Controller\MemberController.class.php<br />class MemberController extends Controller {<br /><br />    //访问地址 index.php?c=Member&a=index<br />    public function indexAction(){<br />        $this->display();<br />    }<br /><br />    //访问地址 index.php?c=Member&a=login<br />    public function loginAction(){<br />        $this->display();<br />    }<br /><br />    //访问地址 index.php?c=Member&a=register<br />    public function registerAction(){<br />        $this->display();<br />    }<br />}<br />
Copy after login



写在最后

最新消息,ThinkPHP 最新版本3.2.x 改正了这几点,Action类改名为Controller。网址中m参数改成了c参,变得直观易懂!
最后感言:几年来一直被MVC困扰,越是去研究ThinkPHP越是觉得一头雾水。一路走来,一路错,还好坚持了,不然真的被搞得彻底放弃了。(欢迎交流 by default7#zbphp.com)






------解决方案--------------------
称为 Action 而不是 Controller 是有道理的
Action 基类 完成了 Controller 的工作
而派生于 Action 的类完成的是  Model (业务模型)的工作
而 Model 类只是数据表的映射

其实认真看一下开发手册就都知道了,何苦要花4年时间?
------解决方案--------------------
愿楼主珍重
------解决方案--------------------
有这么难理解吗?
------解决方案--------------------

雖然菊花一緊,但是我還是認為樓主終於通靈而感到欣慰。
時間長短不要緊,重要的是你終於悟出來了

------解决方案--------------------
主要还是因为,thinkphp是“借鉴”SSH,在SSH里面的MVC就是这样命名的。
------解决方案--------------------
自己简单实现 下MVC整个流程就明白了。
新手也许就3个月一年才能实现。老手实现Thinkphp框架同样功能(mvc主要功能)。也就最多几天或者几个小时。
本php版主估计3小时就足够了吧。
ThinkPHP作者可是php顶尖高手。
ThinkPHP极为简单,而为极为强大,这者作者厉害的地方。
手册也非常完善方便。
------解决方案--------------------
你搞错了,thinkphp 2.x 也就是你说的什么action的,是最清晰的,代码也是最易懂得
现在的thinkphp 3.x,反而变得复杂不易懂了。
------解决方案--------------------
确实很多东西需要悟
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black Sep 04, 2024 am 06:51 AM

DJI has not confirmed any plans to introduce a new action camera yet. Instead, it seems that GoPro will get ahead of its rival this year, having teased that it will introduce two new action cameras on September 4. For context, these are expected to a

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

See all articles