Welcome to FrostSW!

FrostMVC PHP Framework

Documentation   |   Version 1.0.3

controllers/

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.

Creating a Controller

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.

For example:
 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,
  • The class name should be the same as the file name.
  • In order to make a class be recognised as a controller, you should extend the Controller class of FrostMVC.
  • The method index() is a special method that can be used to call the base path of the controller.

Routing your 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,
  • Register your controller by creating a function in routes.php file.
  • The function name will serve as a path name website's URL. For this reason, function name should only contain alphanumeric characters and underscores.
  • You have to declare the class to be used by the routed function using the View::route() function.
  • The $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.
  • Never declare any logic codes and the like to the route function. Such code must only be declared in a controller class.

Testing your Controller

To test your code, open a web browser and enter the following URL:
http://localhost/FrostMVC-master/blog
In the URL above, please note that
  • It is assumed that you are running your web server in localhost (your own machine/computer).
  • The root folder of your source code is assumed as 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.
  • The path /blog refers to the function name in app/routes.php, which is routing the BlogController class.
To learn more about routing, read this article here.

Documentation

  • app/
  • assets/
    • css/
    • img/
    • js/
    • index.php
  • core/
  • libraries/
    • helpers/
      • generalHelper.php
    • vendor/
    • composer.json
    • composer.lock
    • TwigExtensions.php
  • .htaccess
  • index.php
  • php.ini
  • README.md
  • robots.txt