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


这个帖子送给那些被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 为什么会这么难

<?php//文件地址:D:\zbphp.com\www\Lib\Action\MemberAction.class.phpclass MemberAction extends Action {    public function index(){        $this->display();    }    public function login(){        $this->display();    }    public function register(){        $this->display();    }}

每次看到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架构
<?php//文件 D:\zbphp.com\www\Lib\Controller\MemberController.class.phpclass MemberController extends Controller {    //访问地址 index.php?c=Member&a=index    public function indexAction(){        $this->display();    }    //访问地址 index.php?c=Member&a=login    public function loginAction(){        $this->display();    }    //访问地址 index.php?c=Member&a=register    public function registerAction(){        $this->display();    }}



写在最后

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






回复讨论(解决方案)

大神 怎么看待zf啊 

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

其实认真看一下开发手册就都知道了,何苦要花4年时间?

愿楼主珍重

愿楼主珍重


谢谢!

称为 Action 而不是 Controller 是有道理的
Action 基类 完成了 Controller 的工作
而派生于 Action 的类完成的是  Model (业务模型)的工作
而 Model 类只是数据表的映射
其实认真看一下开发手册就都知道了,何苦要花4年时间?



学习了,原来如此。不过如果命名为Controller确实好理解一下。一开始就把m a 往Action.Class 和Model.class上想,所以导致一直都搞不懂ThinkPHP mvc。

有这么难理解吗?


?然菊花一?,但是我?是???主?於通?而感到欣慰。
???短不要?,重要的是你?於悟出?了

思想和框架是两个东西,思想要有,框架不一定要用。

一直觉得ThinkPHP是工厂模式。

主要还是因为,thinkphp是“借鉴”SSH,在SSH里面的MVC就是这样命名的。

主要还是因为,thinkphp是“借鉴”SSH,在SSH里面的MVC就是这样命名的。



今天重新看了一遍,大概的看懂了。不过分布式构架、HTTPSQS、sphinx 这些没用过。
电脑配置太低了,装IDE都CPU 100%

你搞错了,thinkphp 2.x 也就是你说的什么action的,是最清晰的,代码也是最易懂得
现在的thinkphp 3.x,反而变得复杂不易懂了。

一直觉得ThinkPHP是工厂模式。


为啥这么觉得!

其实地址中的m是模块module a是方法action
模块和方法同时定位某个控制器方法,所以说module+action=controller 即地址中m+a=c
而MVC中的m是模型model ,不能用地址来调用,必须系统内部调用的...
你要怪得怪模块和模型的英文都是m开头产生了混淆...

这样说应该清楚明白了:
mvc = model + view + controller
controller = module + action

确实很多东西需要悟

这样子搞 。让我们新手怎么活呢?

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools