-
Notifications
You must be signed in to change notification settings - Fork 16
Controller
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.
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!
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 sayHello
method 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 used:
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 (we assumed localhost is the application 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.
As you can see in the example, for creating a Controller you must write a common PHP Class that uses and extends the abstract framework\Controller
class. Then you must and save it into the directory controllers. By adding public methods to controllers\Home class
, you are able to implement some application logic that can be executed by an HTTP request.
The only skill 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 add:
- 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, and also multiples, values to a method parameters through the browser URL. In other words, you must 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 was defined as public. The reason is that only public methods can be executed.
In fact, 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 sayHelloMessage
and the second one regards the access denied when try to call from the URL a protected method like cantSayHello
.
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.
In the next page, we expose how handling of HTTP requests characterized by the GET and POST methods of HTTP protocol.