In the Flint PHP framework, controllers manage application logic and interact with views or return JSON data when used for API purposes. Here’s an enhanced guide on using controllers effectively:
Controllers are located in the app/controllers
directory and can be structured according to your application’s requirements. Here’s a basic example:
namespace App\Controllers;
class HomeController
{
public function index($params = [])
{
view('home');
}
}
Controller methods can accept parameters, facilitating flexible data handling:
public function index($params = [123, 'imran', 'kashif'])
{
// Controller logic using $params
view('home');
}
Controllers can return different types of responses, such as strings:
public function index()
{
return 'Office Home Page';
}
public function show($id)
{
return 'Showing details for office ID: ' . $id;
}
This flexibility accommodates diverse application needs.
Views are typically stored in the app/views directory and can be rendered using the view function:
public function index()
{
view('home');
}
Optionally, pass data to views using an associative array:
public function index()
{
$data = [
'title' => 'Home Page',
'params' => $params
];
view('home', $data);
}
This dynamically populates views with data.
For API endpoints, you can return JSON data using the json method:
public function apiData()
{
$data = [
'id' => 1,
'name' => 'Flint Framework',
'version' => '1.0'
];
json($data);
}
This method simplifies API response handling by converting arrays to JSON format.
Flint controllers provide structured control over application logic, seamlessly integrating with views or facilitating API responses through JSON. Organize controllers in the app/controllers directory and utilize PHP features to build scalable web applications.
For advanced techniques and further integration options, refer to PHP documentation and Flint framework resources.