Controllers in Aeros handle HTTP requests and return responses. They serve as the bridge between your routes and your application logic, organizing request handling in a clean, maintainable way.All controllers in Aeros extend the base Controller class and are stored in the app/controllers/ directory under the App\Controllers namespace.
Controller methods handle specific actions. They can return views, JSON responses, or any other content:
UserController.php
<?phpnamespace App\Controllers;use Aeros\Src\Classes\Controller;class UserController extends Controller{ public function index() { return view('users.index'); } public function show() { return view('users.show'); } public function create() { return view('users.create'); }}
Routes can point to controller methods using the Controller@method syntax:
routes/web.php
use Aeros\Src\Classes\Router;Router::get('/users', 'UserController@index');Router::get('/users/create', 'UserController@create');Router::post('/users', 'UserController@store');
The framework automatically prepends \App\Controllers\ to the controller name, so you don’t need to specify the full namespace in routes.
Controllers can accept route parameters. The parameter names in the route must match the method parameter names:
routes/web.php
// Define route with parametersRouter::get('/users/{id}', 'UserController@show');Router::get('/posts/{postId}/comments/{commentId}', 'PostController@showComment');
UserController.php
public function show($id){ // $id is automatically injected from the route return view('users.show', ['userId' => $id]);}
PostController.php
public function showComment($postId, $commentId){ // Both parameters are injected in order return view('comments.show', [ 'postId' => $postId, 'commentId' => $commentId ]);}
Parameter names in your controller method must exactly match the parameter names defined in the route (without the : prefix). Otherwise, Aeros will throw an exception.
Use the global request() helper to access request data within controllers:
UserController.php
public function store(){ // Get all POST data $data = request('post'); // Get specific fields $name = request('post', ['name']); $email = request('post', ['email']); // Validate request data $validated = validate([ 'name' => 'required|string|max:255', 'email' => 'required|email', ]); return response(['success' => true], 201, \Aeros\Src\Classes\Response::JSON);}