2. Running a controller - notafrancescodavid/webmvcframework GitHub Wiki
Getting started
Coding and running your first example is extremely simple!
To create a custom controller HelloWorld
create a HelloWorld.php
file in the controllers directory.
Then write a class called HelloWorld
that extends the framework\Controller class.
<?php
namespace controllers;
use framework\Controller;
class HelloWorld extends Controller
{
public function sayHello()
{
echo "Hello World";
}
}
As you can see from the code, to create a controller we need to:
- Use the class
framework\Controller
- Extend it with by writing the subclass
HelloWorld
In HelloWorld
it is defined a method sayHello
that displays a message. In general, a controller has the responsibility to handle the logic and the control flow of a software application. Within WebMVC, frequently invoked methods are defined in the class Controller
located into the directory framework, and that's why HelloWorld extends Controller. Even if in our first example any Controller method is invoked, usually a custom controller take advantage of already available methods.
At this point, the only thing we have to understand is how to instantiate a controller and execute the method.
To do this, open your favorite web browser and type the following address:
http://localhost/myproject/hello_world/say_hello
(Click to run)
You should see:
Hello World
Congratulations, you have executed your first example using WebMVC!
But how exactly it is translated the URL into a controller call?
The URL you wrote calls the sayHello
method of a HelloWorld
class in the controllers directory. First, WebMVC automatically changes the syntax from lower case and underscores to camel case:
hello_world -> HelloWorld
say_hello -> sayHello
Then, after this transformation, the sayHello
method of the HelloWorld
controller is called.
In general, to run a controller method you should use the following syntax:
<your server domain>/<controller name>/<method name>
This schema can be used when you have a single project to manage. In the case of two or more project existing within your web server root, you should use the following syntax:
<your server domain>/<project name>/<controller name>/<method name>
WebMVC and OOP programming
WebMVC requires that you create a Controller that must extend the framework\Controller class. Then, just by adding public methods inside it, you will be able to implement the necessary functionalities. The only knowledge you need is about the OOP programming.
The next example shows some concept regarding the interaction between WebMVC and OOP programming. Specifically, it is about the parameters and visibility of methods in a Controller.
Let us:
- modify the method sayHello enabling it to accept the parameter $message;
- introduce the protected method cantSayHello to the HelloWorld class.
<?php
namespace controllers;
use framework\Controller;
class HelloWorld extends Controller
{
public function sayHello($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/Mark
(Click to run)
http://localhost/hello_world/say_hello/John
(Click to run)
Your output will be:
Hello Mark
Hello John
The invocation of the method sayHello('Mark')
is here possible typing the parameter's value 'Mark' into the URL after the slash that follows the method name. This is the rule for passing one or more parameters to a method: simply specify the corresponding values into URL and separate them with slashes. Take care of passing the exact numbers of values that a method requires as input parameters.
If you try to type:
http://localhost/hello_world/say_hello/Mark/John
or
http://localhost/hello_world/cant_say_hello
in both cases, you will obtain an exception. In the first, you have inserted wrong numbers of parameters for the method sayHelloMessage
, while in the second you have no access to the method cantSayHello
because it is not public.
Summary
This page shows how simple is to start coding with WebMVC and PHP programming. Just design and implement your application writing Controller classes and public methods, and the framework will execute them as common HTTP requests. The URL notation used by WebMVC requires typing the HTTP request writing in lower case the names of Controllers and Methods, by separating them with a slash. It also requires an underscore for separating the occurrence of composite names. 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 in order to avoid tedious operations of configuration.
Whats next
In the next example, we shall focus our attention on the View and the way we can link it to the controller. A View will enable the management of content and graphical structure of a web page.