Controllers

Introduction

By default, all of the controllers for your application are stored in the app/controllers/ directory.

Examples

Here is a basic controller example. In this example we are displaying the Hello World!

// app/Controllers/HomeController.php
namespace App\Controllers;

class HomeController{
    public function index(){
        return "Hello world";
    }
}

We can also display the Hello World using echo or print_r without return

// app/Controllers/HomeController.php
namespace App\Controllers;

class HomeController{
    public function index(){
        echo "Hello world";

        //or

        print_r("Hello world");
    }
}

Specifying in routes

The controller usage is identical to that specified when registering the route.

// routes/api.php
use Ghosty\Framework\Support\Facades\Route;

Route::get('/home')
    ->controller(\App\Controllers\HomeController::class)
    ->action('index');