Skip to content

Controllers

Hüseyin Akbaş edited this page Jan 30, 2018 · 2 revisions

Controllers are one of the most important part of MVC style. So, I made them very easy to use. First of all, you define a controller like this:

<?php
//Filename: app/controllers/AuthController.php
use Just\Core\Controller;

class AuthController extends Controller{
}

controllers directory under the app is reserved to contain controller files. Well, we defined Controller but how we will serve the urls? That's so easy, too. In order to map routes you just define a controller and write functions under it. The framework knows your routes by looking at the controller name and action name. However, if you want to add your custom routes as traditional way it is also possible. See Custom Routes

Defining Controllers

There are some rules to define controllers and its functions.

  • Controllers are defined like FooController
  • Each function in a controller must have post or get prefixes in the beginning. For ex. getBar
  • Index functions are reserved to serve / directories and unless defined, HomeController -> getIndex is the main page.
use Just\Core\Controller;
class AuthController extends Controller{
     //This serves GET site.com/auth
    public function getIndex(){
     //...
    }

    //This serves POST site.com/auth/create
    public function postCreate(){
     //...
    }
}

There are also two system functions reserved in order to use filters and custom stuff. One of them is beforeAction and the other one is afterAction they are called by framework before and after the route function called consecutively.

Clone this wiki locally