Recommend this page to a friend! |
Classes of Saro Carvello | PHP Web MVC Framework | wiki/Understanding-WebMVC.md | Download |
|
![]() IntroductionThis section provides you with a high-level overview of Model, View, and Controller of WebMVC and how it works to generate web pages. After reading this introduction, you should understand how the different parts of a WebMVC application are physically and logically organized and how they work together. You should also understand how the architecture of a WebMVC application differs from a standard PHP application. Basic organization of a WebMVC applicationBuilding a WebMVC application requires you to put your custom code into four special sub-folders of the server application root folder: models, views, templates, and controllers. As you might guess from the folder names, these folders physically organize the classes for implementing models, views, and controllers. In addition to structuring the physical organization of files, these folders also represent the logical organization of Namespaces in which each class will be encapsulated. About the templates folder, the only thing you must know is that it will contain the HTML code that will be used by WebMVC PHP classes to generate the dynamic page of your application. The figure below shows the tree structure for folders: Load and Dispatch by "Convention over Configuration"When you build a traditional PHP application, there is a one-to-one correspondence between a URL and a PHP page. If you
make an HTTP request like When building a WebMVC application, in contrast, there is no correspondence between the URL that you type into your browser's address bar and the files that you find in your application. _In a WebMVC application, a URL corresponds to a controller action instead of a page on disk_. > In a traditional PHP application, browser requests are mapped into pages. In a WebMVC application, in contrast, > browser requests are mapped to controller actions. A PHP application is content-centric. A _WebMVC application_, in > contrast, _is application logic centric_. In WebMVC any browser request gets mapped to a controller action through a feature called _Loading_ and * _Dispatching_*. This feature is built on architectural design named "_Convention over Configuration_" to automatically handle incoming HTTP requests and routes them to the corresponding controller actions. We will give you later more technical details about this feature in Controller Page, <a href="Understanding-Controller"></a>Understanding ControllerA Controller is responsible for controlling the way that a user interacts with a WebMVC application. A controller
contains the flow control logic for an application. A controller determines what response to send back to a user when a
user makes a browser request.
Technically speaking a controller is just a concrete class of the abstract Understanding View and TemplateThe View has the responsibility to organize and show data in graphical structures.
The development of a View in the web environment, unlike what happened for desktop applications, involves the use of
different programming languages: some server-side, like PHP, and other client-side, like HTML, CSS, and JavaScript. In
WebMVC these differences are used respectively by involving two distinct entities rather than just one as was expected
by the MVC pattern, originally designed for desktop applications. These entities are the Understanding ModelThe Model handles the state of the application. The state is what your application is about. In WebMVC model is provided
by the Handling incoming HTTP requestsNow you learned that WebMVC, as a result of an HTTP request, loads and runs a Controller, connected in turn with the View and with the Model. Below, we show a flow diagram to illustrate you the interaction of all these entities: The flow description is the following;
Naming conventionWebMVC requires that you must mandatorily use PascalCase and camelCase notation for naming, respectively, classes and methods. Furthermore is strictly recommended (but optional) to use a unique name for all MVC parts. For example, if you are designing the home page of your site, you can use the name "Home" for all classes such as the Controller, View, Model, and the name 'home' for the Template, You must also use the name 'Home' for many other files, such as language translation files, which we will show you later. See the following figure: > From a conceptual point of view, this means that you can specify a unique name that identifies the MVC triad > cooperation. In this example, we call it _Home_ and it identifies a WebMVC assembly that will be generated at > runtime for the aggregating of the MVC triad and the template. > Summarizing, a WebMVC assembly identifies an entity generated at runtime that will provide a primary service (typically > a web page) and we can identify by a name. Then we can use this name for physically naming the MVC triad and template > that cooperate together for producing the service. The name of a WebMVC assembly will match the Controller name of the > triad and will be also used by the framework for providing to you an endpoint you will use for consuming the service. Although we can use the same name for the different MVC classes triad, the unique identification of a single class name still will be possible because WebMVC uses the PHP namespaces for encapsulating each class; this convention allows avoiding naming conflicts and the proliferation of names in complex projects. We will provide more details about using namespaces when they will be used for managing subsystems for decomposing the system. Insights WebMVC assemblyGenerally, a WebMVC assembly, that inherits its name from Controller (alias root Controller), can furthermore aggregate together many more parts, rather than only its related Controller, Model View, and Template. In fact, it can assemble, into a hierarchical structure, many more parts like nested MVC triad and/or Components. All those nested elements may be also considered as child MVC assemblies. We will give you more technical details later about the hierarchy of MVC assemblies when introducing Content Decomposition and Components. By looking at the figure above, in which we used a WebMVC Assembly name '_Home_', see the positioning of the Note that the convention we discussed so far, of using a single name for all the main parts of MVC assembly, is not
mandatory. If you prefer you can also decide to name classes and templates by using the traditional suffixes,
eg. SummaryWe exposed the basic concepts you need to understand before using WebMVC framework for developing an application. They are:
So you simply:
What's nextAfter the explanation of the main architecture of WebMVC, and by understanding its basic structure we are now ready to start coding its first entity: the Controller. |