The Controller serves as the brain of the MVC. It is responsible for how the Views will be presented and how the data will be passed to the Models. Controller basically a class that contains the logics on how the user will interact with your application.
To create a controller, you can simply create a php file (a file with .php extension) and name it according to the purpose of your controller
and save it to app/controllers
directory.
BlogController.php
Add the following code to the BlogController
controller:
public class BlogController extends Controller{
public function index(){
print '<h1>Hello World!</h1>';
}
}
In the codes above,
Controller
class of FrostMVC.
index()
is a special method that can be used to call the base path of the controller.
Once you are done with the declaration of your controller, locate the file app/routes.php
and register your controller to the routes by adding the following codes:
function blog($pages){
View::route('BlogController', $pages);
}
In the codes above,
View::route()
function.
$pages
parameter is a required parameter and is internally used for processing URL values and paths. It should always be declared and passed to the View::route()
function.
http://localhost/FrostMVC-master/blogIn the URL above, please note that
FrostMVC-master
. If you changed the folder name, please use your folder name instead. Note that it is better to use a virtual host than accessing the localhost directly./blog
refers to the function name in app/routes.php
, which is routing the BlogController
class.