开发者

Trying to understand MVC - am I already doing it?

开发者 https://www.devze.com 2023-02-22 00:33 出处:网络
I\'m quickly getting knee-deep in larger and larger applications so I thought it\'d be a good idea to learn what patterns are and how they work, etc.So, I\'m watching some tutorials on youtube and rea

I'm quickly getting knee-deep in larger and larger applications so I thought it'd be a good idea to learn what patterns are and how they work, etc. So, I'm watching some tutorials on youtube and reading some books on patterns and design principles and MVC comes up. I'd heard of it but never really understood it and have yet to use Zend or CodeIgniter or the others that use it intrinsically.

Anyway, in the process of studying the topic of MVC, it seems as if I'm already using it. Can someone verify? Here is how I normally go about setting up an application using a login system as a for instance:

login.php - this is the html page that shows the form. The form submits to:

process.php - this class checks the value of submit and based on the value, validates inputs, checks password, sends back applicable erros, if everything is kosher it instantiates a User object:

user.php - this is a class that templates the properties and methods a user will need including things such as adding the user to the database, pulling profile information, logging out, instantiating a private message object from the user, sets开发者_如何学JAVA the session variables needed, etc. Information is then returned to process.php which then header redirects back to the main page with a successful login.

So...is this a simple MVC? In the studying I've done, login sounds like view, process sounds like controller, and user sounds like model. I'm sure there's a bit more in-depth here, but I'm really trying to wrap my head around this stuff so any adjustments to my logic are more than welcome. Thanks.


MVC is a way of keeping your three main parts of the application separate for the purposes of increasing maintainability and usually with it, performance.

For example, if you're using PHP 5.3 (you should be, late static binding, namespaces etc are very useful!), you can use a namespace for each of the sections of your application. Then, when you have an error in your application logic, you know exactly that you need to go to the model for a particular action and fix it.

Views allow us to keep display logic separate from the business logic of the application. Some might use XHTML with inline <?php echo $myVar; ?> kind of things, while some may prefer templates. This means when you edit the GUI or other outward facing part of your application, it will never have any impact on your business.

It means all your code is organized and increases maintainability, while also modularizing your application. It's even better if you use an autoloader (always use spl_autoload_register('myAutoloader') rather than function __autoload($className) as this can be overriden by libraries and things) so your app only loads what's needed, when it's needed.


In short yes, what you have is the barebone basics of the MVC pattern.

One commonly mentioned phrase in the MVC paradigm is "fat model, skinny controller". Generally your validations should be done in the model object. The controller should be as light as possible.


Your 3 pages sounds pretty much like MVC. It's always helpful, however, to use a framework like Codeigniter or Zend to make you write more rigorous and maintainable code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号