If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

MVC Design Pattern in PHP

Started by clickinfra, May 24, 2023, 07:41 AM

Previous topic - Next topic

clickinfraTopic starter

Can you suggest any PHP resources for studying the MVC design pattern? I've searched the internet for a week without success.

If you have experience with MVC in PHP, please share where to begin and where to find simple explanations on this topic.

I've come across a lot of information on ASP.NET, but I'd like to learn through PHP examples.

Additionally, are there any other design patterns similar to MVC that you would recommend exploring?
  •  


jesusbond

What is the meaning of MVC analogues? Other variations of MVC, such as MVVM or MVP, exist (although they are essentially the same). There is also a 3-tier architecture, but it is unlikely that you will want to use it in PHP.

I learned about MVC by using CakePHP as an example (although I do not particularly like the framework, it serves as a useful training tool). Simply read the accompanying manual, which provides a step-by-step example of how to create a blog using the MVC approach.

You do not need to use patterns yet. All patterns are described in a book by Martin Fowler (a list can be found on martinfowler.com/eaaCatalog/, although the Russian translation is poor), but in order to understand them, you must first have some experience writing code and analyzing someone else's code. Without this experience, these patterns will seem foreign and incomprehensible to you (lots of clever words, but why bother if you can write it in a simpler way).

If you are (unexpectedly) interested in patterns in order to write more precise, high-quality, professional, and maintainable code, it is better to follow generally accepted principles:

- DRY (Don't Repeat Yourself) - there should be no repeating pieces of code longer than a few lines, no code generation by copying and editing, and some data (such as a list of countries) should be stored in only one location, rather than several. Following this principle improves the architecture of the code and simplifies maintenance.
- KISS (Keep It Simple, Stupid) - choose the simplest way to implement something if there are several options, and if it does not pose problems in the future.
- Do not rely on features from PHP4. It is outdated.
- Do not rely solely on HTML5/CSS3. Not everyone has iPads with Macs, and IE8, IE7, and IE6 are still in use.
- Write your code as if it will be maintained by a psycho maniac who knows where you live. In other words, do not write things that are difficult or impossible for someone else to understand. Think about who will read your code. Occasionally place comments in difficult areas. Do not spread the logic of an action across 10 files, and do not make files larger than 500-1000 lines.

- Code written in functions can be easily converted into OOP code by turning functions into static methods and combining them into classes.
  •  

Mississauga

Django implements MVC architecture effectively with Model representing business logic, data processing and storage. Controller is referred to as templates in Django and its main task involves preparing data for the view. View, on the other hand, should only focus on presentation and not have any business logic.

It's worth exploring other frameworks such as Yii and Symfony 2 to see how they implement MVC architecture as well.
  •  

eetplus

Instead of following MVC or other specific alternatives, consider a more encompassing paradigm called Layers. This approach involves separating different layers or parts of the application into independent modules that can be decomposed and composed as needed. It's important not to adopt trendy techniques like MVC without considering the wider context and potential drawbacks that may arise.

In software development, it's easy to get caught up in popular methodologies without fully understanding their underlying principles and implications. By utilizing a Layers approach, developers can create a more flexible and adaptable architecture that better meets the needs of their project. This involves breaking down the system into smaller components that can be easily managed and maintained over time. So, instead of blindly following the crowd and implementing trendy techniques, take a step back and consider a more comprehensive approach like Layers.
  •  

zakladykliczko

To start learning MVC in PHP, I recommend beginning with the official documentation of popular PHP frameworks such as Laravel, Symfony, or CodeIgniter. These frameworks are built around the MVC pattern and provide comprehensive documentation and tutorials to help beginners understand the concept.

Laravel, for instance, has an excellent documentation section specifically dedicated to explaining the MVC architectural pattern and its implementation within the framework. The "Laravel - MVC Framework for Web Artisans" section on the official Laravel website is a great resource to begin your journey into MVC with PHP.

In addition to official framework documentation, there are various online tutorials and courses available on platforms like Udemy, Coursera, and Pluralsight that specifically focus on teaching MVC in PHP. These resources often provide practical examples and real-world projects to help you grasp the concept more effectively.

If you prefer written resources, "PHP 5 CMS Framework Development" by Martin Brampton is a book that provides a clear explanation of the MVC pattern and demonstrates its implementation in PHP through building a content management system.

As for other design patterns similar to MVC, I would recommend looking into the following:

1. MVVM (Model-View-ViewModel): This pattern is commonly used in front-end development, especially with JavaScript frameworks like Knockout.js and Vue.js. It focuses on separating the UI logic from the business logic, similar to MVC.

2. Observer Pattern: This pattern is used to define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. It's useful for building systems where changes in one part of the application need to trigger actions in other parts.

3. Dependency Injection: While not a traditional design pattern, dependency injection is a widely used technique in modern PHP development, especially with frameworks like Symfony. Understanding dependency injection is crucial for building maintainable and testable applications.

Exploring these patterns alongside MVC will give you a broader understanding of software design and architecture, allowing you to make informed decisions when building PHP applications.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...