MVC - Controller class tutorial [closed]

2019-03-17 02:16发布

问题:

I am looking for a tutorial / book that guides me to understand the Controll functions and the best practices to write my own controller + Model

Thanks in advance.

回答1:

Im sure theres plenty of links been posted to get you started but some important factors in creating an MVC is:

  • Static Registry Class (Store objects and fetch with a global scope)
  • Router (A class that determains the controller/method and params from URI's)
  • Base Controller (just a small abstract class then the users controller can extend)
  • SPL Auto-loading (this will allow users to extend classes such as Model_Database)
  • Structure (you should create directories in accordance with names, I.e Library_session would load /library/session.class.php)
  • Model Abstraction (Account for all types of storage, Database, Disk etc)
  • Error Tracking (Always make sure your logging and capturing errors)

They are just a few tips and ideas you should be thinking about when you create your system.

What you should also do is user other frameworks and build some sample projects, learn how an MVC Framework should be sued, so when your building one you know what the user should expect, then just really study the core structure of the framework.

Take into consideration in PHP the following are usually how MVC Works

  • Controller (this is executed depending on the URI)
  • Model (Accessed from the controller and should be the I/O of Data)
  • View (Templates basically)

but you can work with a MVCL which is (M odel/V iew/C ontroller/L language)

Language is not a specific in the original documentation but its been adopted a few times in regards to the pattern structure, An example of the file structure below will guide you into whats the main purpose of the +L

M: \catalog\model\catalog\product.php
V: \catalog\view\template\product\product.tpl
C: \catalog\controller\product\product.php
L: \catalog\language\english\product\product.php

An example of what company / project uses this method is: OpenCart, AND I HIGHY RECOMMEND YOU LOOKING AT THE ARCHITECTURE!



回答2:

Here are some useful links,

  • Model View Presenter in MSDN Magazine (To know the difference b/w Presenter and Controller look at Martin Fowler's: GUI Architecture)
  • MVC from Oracle
  • Another MVC from CodeProject
  • Building Graphical User Interfaces with the MVC Pattern


回答3:

Here is a hopefully helpfull link to an article.

and here is a link to a very descriptive tutorial.



回答4:

Why not trying codeigniter? It is a Model View Controller based Framework.

In combination with doctrine its pretty usefull.

Here the link to some codeigniter tutorials: codeigniter tutorials



回答5:

You're question basicaly a design patern question, a realy good book about this subject is:

Architect's Guide to PHP Design Patterns