Controllers

Manually Create

Follow these steps to create a new controller in PHPFast:

  1. Go to the application/Controllers directory.

  2. Create a new PHP file and name it according to your controller, e.g., HomeController.php.

  3. Define the controller class by extending System\Core\BaseController to inherit core framework functionality.

Here is a basic example:

<?php
namespace App\Controllers;

use System\Core\BaseController;

class HomeController extends BaseController
{
    public function index()
    {
        // Fetch and display a home page
        echo "Welcome to PHPFast";
    }
}

Using the Command-Line Interface

This command generates a new controller file in the application/Controllers directory.

  • It creates a basic controller template, helping you set up new features faster.

Command:

This creates UsersController.php inside application/Controllers

Controller Structure

Basic Controller

Advanced options

When you define the UsersController inside the Controllers/backend/ folder and use the UsersModel

The structure looks like:

And UsersController:

Routing to a Controller

To route a URL to a specific controller method, use:

Basic

  • URL: /users

  • Calls: index() method in UsersController

Dynamic Parameters

  • URL: /users/123

  • Calls: show(123) method in UsersController

Models in Controller

Controllers can interact with Models to handle database operations.

Below is an example of how to load a model and use it within a controller:

Render Views & Assets Data to Views

Load CSS and JS File

Get data from Controller

Manual way

Using Block

In Blocks/Head/Views/default.php

In Blocks/Footer/Views/default.php

Message Handle

Output:

Caching in Controller

Last updated

Was this helpful?