Augustin stack
Augustin stack

Reputation: 82

php MVC concept advantages

I have implemented applications using normal php and php classes,Can anyone explain simply the exact concept, difference and advantages of MVC architecture ,I have read many articles about it,But I am looking for a simple answer,Please

What is the importance of MVC in php?

Upvotes: 1

Views: 4963

Answers (3)

Ravi Chauhan
Ravi Chauhan

Reputation: 1458

- Code and File Organization

Just because you created an “/inc” folder and made a “functions.php” file does not mean your code is organized. When you setup a PHP Framework, it already has a certain folder structure. It is expected from you to follow the same standards and keep everything organized in a certain way. Once you get used to this model, you will never want to go back. Unfortunately for some command line champions that still use vi, this can be a challenge. You will need to work with more files, that are smaller in size. But when you use a decent modern code editor or an IDE, it will be a breeze to browse through your application code and find what you need, quickly.

- Utilities and Libraries

PHP is a great language for web development and provides countless number of tools and libraries. However, if you ever try to build a whole website with PHP alone, you will find yourself either hunting down a lot of 3rd party code and libraries, or have to write them yourself. All top PHP frameworks come with certain Libraries and Helpers, that help you with: Form Validation Input/Output filtering Database Abstraction Session and Cookie Handling Email, Calendar, Pagination etc… The list goes on. Not to mention, there is plenty of plugins provided by the community that you can add to your framework.

- Security

In PHP you can already find many input and output filtering functions to protect your website against certain attacks. However, manually using these functions can get tiring and you may forget about them every once in a while. With a framework, most of the work can be done for you automatically. For example in CodeIgniter:

  • Any value passed to database object gets filtered against SQL injection attacks.
  • All html generating functions, such as form helpers and url helpers filter the output automatically.
  • All user input can be filtered against XSS attacks.
  • Encrypting cookies automatically is only a matter of changing a config option.

-Less Code & Faster Development

There is of course a learning curve for all PHP Frameworks. But once you get over this hump, you will enjoy the benefits of rapid application development.

You will write less code, which means less time spent typing. You will not have to chase down 3rd party libraries all the time for every new project because most of them will come with the default framework install.

Also, since you are being more organized, it will be much faster to chase down bugs, maintain code, and make changes to existing code.

-Performance Tools

One of the main arguments from the naysayers comes in this subject. There is obviously a performance hit when you build a “Hello World” application with a framework vs. plain PHP code.

But those benchmarks are just bad examples. First of all, you should understand that developers are more expensive than servers. Saving time from development and maintenance is likely to outweigh any extra money you need to spend on servers.

Putting all of that aside, you can actually gain performance benefits by using a PHP framework. They come with tools that help you do caching, benchmarks, profiling etc…

Modern frameworks are also great with dynamic loading, as they load code only as needed. Different page requests can load different amount of library code based on what needs to be used.

-Suitable for Teamwork

The way your project is organized in a PHP Framework also helps you create a suitable environment for teamwork.

You can let your designers work on the Views, database guru work on the Models, let the smart programmer (yourself ;) ) build reusable Libraries and Plugins etc…

Also you can let someone build unit tests, because they come with tools for that too.

And It’s Fun!

Upvotes: 3

Doh-a
Doh-a

Reputation: 91

In MVC your model, your view and your controller are differents parts.

So you can change your view without changing your controller and your model.

For example : you have all the informations you wan't in your site, you jsut wan't to change the design. You don't have to do any changes in the model code, change change the view aprt.

That's the same idea in php templates, but works for the three part.

Upvotes: 3

dknaack
dknaack

Reputation: 60468

MVC is basically "seperation of concern"

The MVC pattern separates an application in 3 modules: Model, View and Controller:

The model is responsible to manage the data; it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.

The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla, …

The controller handles the model and view layers to work together. The controller receives a request from the client, invoke the model to perform the requested operations and send the data to the View. The view format the data to be presented to the user, in a web application as an html output.

Check out:

Upvotes: 5

Related Questions