Skip to content

Controller

Rosario Carvello edited this page Jan 14, 2018 · 46 revisions

Introduction

Welcome!
You are going to learn how fast is to start coding with the WebMVC framework.
We have already discussed the role of the controller in the MVC design pattern here. As follow of that, for designing and implementing your first application you only need a basic knowledge of OOP programming for writing special classes: the Controllers. In fact, by writing a controller, also equipped with some public methods, WebMVC will enable you to instantiate and execute it's logic, simply by typing special HTTP requests from your browser.

Let's start coding

Coding and running your first controller is extremely simple!
Just write and save the following file HelloWorld.php into the controllers folder:

<?php
namespace controllers;

use framework\Controller;

class HelloWorld extends Controller
{
    public function sayHello()
    {
        echo "Hello World";
    }
}

The figure below show you the path for HelloWorld.php. Note that you must use the PascalCase notation for naming a controller.

At this point, the only thing you have to understand is how to instantiate the HelloWorld Controller and execute its sayHello method. Just open your favorite web browser and type the following address:

http://localhost/hello_world/say_hello (Click to run)

You should see:

Hello World

Congratulations, you have just developed and executed your first application using WebMVC!

Explanation: How coding and executing Controller

WebMVC requires you code a custom Controller for implementing application logic. In general, a controller has the responsibility to handle the logic and the control flow of a software application. To do this you must create a PHP Class that extends the framework\Controller and you must save it under the controllers directory. That is what we have done with HelloWorld controller. Then we ran it by requesting it from browser URL that's mean we typed its name and its sayHellomethod by using a WebMVC notation. This notation is very intuitive because it mirrors the PascalCase or camelCase notation that must be mandatory used when coding classes or methods. It simply consists of typing the HTTP request by specifying both the Controller and its method name to execute in lower case and by separating them with a slash. It also requires an underscore for separating an eventual occurrence of composite names for the controller or for methods names. In fact, we typed:

hello_world -> for specifying HelloWorld Controller that must mandatory named by using the PascalCase notation

say_hello -> for specifying its sayHello method that must be mandatory named by using the camelCase notation

then we assembled controller ad method into an HTTP request (by assuming localhost as the application's server root):

http://localhost/hello_world/say_hello

That's it all.

Therefore, you don't need to configure the execution of a particular Controller, but you just use the URL notation proposed by WebMVC. This simplicity derives from the convention over configuration approach that the framework uses for object instantiation and method invocation in order to avoid tedious operations of configuration for running the controllers. The convention over configuration mechanism used by WebMBC is simple: as we just said before, you must mandatorily use the PascalCase and camelCase notation when naming, respectively, classes and methods.

Insights: Controller and object methods

As you can see in the example to create a Controller you must create a common PHP Class that use and extends the abstract framework\Controller class and save it into the directory controllers. Then just by adding public methods inside it, you will be able to implement some application logic ready to be executed. The only knowledge you need right now is about the OOP programming. For this purpose, the next example will show you other concepts regarding the interaction between WebMVC and OOP programming. Specifically, they are about parameters and visibility of Controller methods.
Look and run the code below in which to the previous example we added:

  • a new public method that accepts parameter: sayHelloMessage($message)
  • a new protected method: cantSayHello():
<?php
namespace controllers;

use framework\Controller;

class HelloWorld extends Controller
{

    public function sayHello()
    {
        echo "Hello world";
    }
	
    public function sayHelloMessage($message)
    {
        echo "Hello $message";
    }

    protected function cantSayHello()
    {
        echo "This method cannot be called from Url";
    }
}

Then type the following address:

http://localhost/hello_world/say_hello_message/Mark (Click to run)

The output will be

Hello Mark

Also type:

http://localhost/hello_world/say_hello_message/John (Click to run)

The output now will be:

Hello John

As you can note we requested the execution of the method sayHelloMessage and for specifying a value Mark (or John) for its single parameter $message we simply typed its value into the URL. We also used a slash for separating the requested method name say_hello_message and the value for its parameter. This is the WebMVC convention for passing one, or also multiple, values to a method parameters through URL. In other words, simply specify the values, corresponding to the parameters, into URL and separate them with slashes. But, take care of passing the exact numbers of values that a method requires as input parameters otherwise, an exception will be thrown. You further note that the requested method is defined as public. We will discuss this aspect later

Instead, if you try to type:

http://localhost/hello_world/say_hello_message/Mark/John
or also
http://localhost/hello_world/cant_say_hello

By running this requests, in both cases, you will obtain an exception. The first exception is about the wrong numbers of values, Mark and John, both we passed to the single parameter $message designed into the method sayHelloMessageand the second one regards the access denied when try to call from the URL a protected method like cantSayHello.

Summary

This page exposes to you how simple is to start coding with WebMVC in accordance with the OOP programming. Just design and write your application in terms of concrete Controller classes and public methods. Then WebMVC let you execute them as common HTTP requests. This technically means that every valid HTTP request from the user will match the execution of an action managed by a controller.

Whats next

In the next page, we expose how handling HTTP requests characterized by its GET and POST methods

Clone this wiki locally