Controllers

Controller is the 'C' of MVC. Although it comes last in the word MVC, but in actual MVC architecture it is the first thing that comes before V (View) and M (Model). Controller is nothing but a PHP class that contains the business logic. A Controller can make query to database or it can load a model that makes query to database. It can load a view file to render markup.

In route file you define which HTTP Request will be handled by which Controller. Accordingly whenever a request is made, PHPasap will check if any rule is defined for current request. If yes then it will call the corresponding Controller method. So say in routes.php we have below rule

Route::add("GET", "welcome", "Pages_Controller@welcome");

Whenever any request is made to http://some-domain.com/welcome then PHPasap will create an instance of Class Pages_Controller and call the welcome method on this instance.

Controllers in PHPasap

In PHPasap all controllers live in app/controllers/ directory. A controller should always be namespaced under namespace app/Controllers. Below is an example

namespace app\Controllers;

class Pages_Controller {
    
}

Calling a View from Controller

You can use any view in your Controller without doing anything special just by calling View::make() or View::render(). Note that render() returns html string while make() returns an instance of View_Handler class. Controller method that handles any HTTP Request should always return a string or View_Handler object. For now remember that you are suppose to return the value returned by render() or make() and NOT echo it.

namespace app\Controllers;

class Pages_Controller {
    
    public method welcome() {
        return View::make("modules/welcome");
    }
    
}
Controller methods responding to HTTP Requests

It is expected that all the methods that respond to any HTTP request, like method welcome in above case, should return any of the below:

  1. String
  2. View Object
  3. Request Object
Of course you may anytime echo something for debugging.

Calling a Model from Controller

All model files should reside in app/models directory. You are however free to change this directory structure and are free to keep your model files anywhere as long as you follow proper naming convention and use proper namespace. To use a model you will have to use its full name including the namespace.

So say we have a user.php file in models directory containing our User model. Then to use it in our Pages_Controller below is how we would go

namespace app\Controllers;

//All models must be namespaced under app/Models
use app\Models\User

class Pages_Controller {
    
    public method welcome() {
        $user = new User();
        return View::make("modules/profile", ["user" => $user->get_profile_details()]);
    }
    
}

Calling a Controller from Controller

Although as per standards of MVC you should not call a Controller from another Controller, still if your application demands such requirement, you can anytime call the Controller by directly using its name. Since all Controllers are namespaced under app/Controllers you just have to reference the class name.

Below is an example

namespace app\Controllers;

class Pages_Controller {
    
    public method welcome() {
        
        //Users_Controller is in same dir as Pages_Controller
        $users_controller = new Users_Controller();
        
        //rest of the code
    }
    
}