Controllers
Manually Create
Follow these steps to create a new controller in PHPFast:
Go to the
application/Controllersdirectory.Create a new PHP file and name it according to your controller, e.g.,
HomeController.php.Define the controller class by extending
System\Core\BaseControllerto 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:
/usersCalls:
index()method inUsersController
Dynamic Parameters
URL:
/users/123Calls:
show(123)method inUsersController
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?