search
HomeBackend DevelopmentPHP Tutorial 帮帮小弟我理解一上PHP的MVC模式

帮帮我理解一下PHP的MVC模式
1、单一入口:是不是前台的没一个用户请求都是从这个入口进来的?
这个入口一般都是index.php,那这个网址怎么弄得?
2、它是一个模块对应一个控制器吗?假如有一个用户模块,里面包含注册、登录等等,在到控制器之前是不是有一个解析网址的过程?解析网址识别用户的请求,假如是一个登录请求,那就转到用户控制器,然后控制器再把请求转到模型,不知道是不是一个模块就写一个类(模型),里面包含注册、登录等等方法,还是一个模块对应一个控制器,模型里面就是分开的注册、登录等类?
3、看了老半天框架和一些mvc写的项目也没有看出MVC是怎么工作的,一会跳到哪里一会又跳到这里,真搞不懂这流程。

------解决方案--------------------
1、单一入口:是不是前台的没一个用户请求都是从这个入口进来的?
这个入口一般都是index.php,那这个网址怎么弄得?

一般通过.htaccess 文件控制转到入口文件,当然还有其他办法

2、它是一个模块对应一个控制器吗?假如有一个用户模块,里面包含注册、登录等等,在到控制器之前是不是有一个解析网址的过程?解析网址识别用户的请求,假如是一个登录请求,那就转到用户控制器,然后控制器再把请求转到模型,不知道是不是一个模块就写一个类(模型),里面包含注册、登录等等方法,还是一个模块对应一个控制器,模型里面就是分开的注册、登录等类?

并不是一个模块对应一个控制器,一个模块可以对应多个控制器,这个可以自己根据业务来设计,在到控制器之前确实有个解析网址的过程,这个过程就是在上面提到的单一入口里面进行的,解析网址就是为了确定使用哪个控制器。控制器接受用户数据并调用模型处理数据,然后控制器又把模型处理得结果发送到视图(比如注册登录,控制器接受用户名密码等,然后调用模型对用户名密码进行验证等,然后控制器将验证等的结果(成功或失败)又发送到视图)。你说的注册登录实际上是控制器里面的方法(一般叫做action),只不过注册登录的一些逻辑(比如查询数据等)是在模型里面完成的。


3、看了老半天框架和一些mvc写的项目也没有看出MVC是怎么工作的,一会跳到哪里一会又跳到这里,真搞不懂这流程。
框架里面的简单的流程一般都是 (当然中间还有些其他东西,这个只是大概的)
请求->转到单一入口->路由(也就是你说的解析网址)->分发->处理请求->完成
------解决方案--------------------
M:model 模型
V:view 视图 
C:control 控制

3、看了老半天框架和一些mvc写的项目也没有看出MVC是怎么工作的,一会跳到哪里一会又跳到这里,真搞不懂这流程。
“瞎子摸象”,这就对了!
框架隐藏了模块间的联系,只将一些可以被改变的环节暴露给用户。在框架里编写程序,如同回答填空题。
这也就是不建议初学者使用框架的原因

2、它是一个模块对应一个控制器吗?假如有一个用户模块,里面包含注册、登录等等,在到控制器之前是不是有一个解析网址的过程?解析网址识别用户的请求,假如是一个登录请求,那就转到用户控制器,然后控制器再把请求转到模型,不知道是不是一个模块就写一个类(模型),里面包含注册、登录等等方法,还是一个模块对应一个控制器,模型里面就是分开的注册、登录等类?
MVC的缺点是分工不明确
现在大多框架都将业务逻辑放在 C 里,将数据表操作 放在 M 里
而 MVC 的字面含义中,C 只负责控制,与业务逻辑无直接关系
如果你使用现有的框架那么就没有编写 M 一说了。M 在框架创建项目时根据涉及的数据库自动完成编程。你的精力只是在将业务逻辑分解成一个个的控制器

1、单一入口:是不是前台的没一个用户请求都是从这个入口进来的?
这个入口一般都是index.php,那这个网址怎么弄得?
单一入口就是所有的用户请求都从一个接口文件进入,所有用户的需求都化为参数传入

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
PHP vs. Python: Understanding the DifferencesPHP vs. Python: Understanding the DifferencesApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting?PHP: Is It Dying or Simply Adapting?Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The Future of PHP: Adaptations and InnovationsThe Future of PHP: Adaptations and InnovationsApr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

When would you use a trait versus an abstract class or interface in PHP?When would you use a trait versus an abstract class or interface in PHP?Apr 10, 2025 am 09:39 AM

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

What is a Dependency Injection Container (DIC) and why use one in PHP?What is a Dependency Injection Container (DIC) and why use one in PHP?Apr 10, 2025 am 09:38 AM

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Apr 10, 2025 am 09:37 AM

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

How does PHP handle file uploads securely?How does PHP handle file uploads securely?Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?Apr 10, 2025 am 09:33 AM

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools