Routing

Configuration

To specify the location of the file with routes, change the routes parameter in the configuration file along the path config/routing.php

The path to the folder with routes is specified relative to the root directory of the project, for example:

// config/routing.php
// Path to routes file from base dir
// default: '/routes/api.php'
'routes' => '/routes/api.php'

You can also specify a route description method, for example:

// config/routing.php
// If true routes file should return a RouteStack object.
// If false, the singleton RouteStackContract is used.
// default: false
'return' => true,

In this case, the routes/api.php file should return an object of type RouteStack for example

Default Route Registration

To register routes this way, you need to set the return parameter in the config/routing.php configuration file to false:

// config/routing.php
// If true routes file should return a RouteStack object.
// If false, the singleton RouteStackContract is used.
// default: false
'return' => false,

Basics

Using Route Facade

In this example, we define a GET /user route using the Route facade, this route will be processed by the TestController::show() method

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

Route::get('/user')
    ->controller(\App\Controllers\TestController::class)
    ->action('show');

Using Service Container

In this example we define a route GET /user ServiceContainer this route will process the TestController::show() method.

// routes/api.php
/**
* @var RouteStackContract
*/
$RouteStack = Container::make(RouteStackContract::class);
$RouteStack->push(new Route(
    "GET",
    "/user",
    \App\Controllers\TestController::class,
    "show"
));

Parameters

Using Route Facade

In this example, we use the Route facade to define a GET /user/{id} route that will process the TestController::show() method, where {id} is a dynamic parameter:

You can pass parameters either as an array or as a Ghosty\Component\Routing\Contracts\Bags\ParameterBag object

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

Route::get('/user/{id}')
    ->controller(\App\Controllers\TestController::class)
    ->action('show')
    ->parameters(['id']); // or ->parameters(new ParameterBag(['{id}']))

Using Service Container

In this example, we use ServiceContainer to define a GET /user/{id} route that will process the TestController::show() method, where {id} is a dynamic parameter:

In this case, to define parameters, you must only pass an object of type Ghosty\Component\Routing\Contracts\Bags\ParameterBag

// routes/api.php
/**
* @var RouteStackContract
*/
$RouteStack = Container::make(RouteStackContract::class);
$RouteStack->push(new Route(
    "GET",
    "/user/{id}",
    \App\Controllers\TestController::class,
    "show",
    new ParameterBag(['{id}'])
));

Middlewares

Using Route Facade

In this example, using the Route façade, we register the POST /user route, which will process the TestController::store() method, and we also define the TestMiddleware middleware for this route

You can pass middlewares either as an array or as a Ghosty\Component\Routing\Contracts\Bags\MiddlewareBag object

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

Route::post('/user')
    ->controller(\App\Controllers\TestController::class)
    ->action('store')
    ->middlewares([TestMiddleware::class]); 
// or ->middlewares(new MiddlewareBag([TestMiddleware::class]))

Using Service Container

In this example, using the ServiceContainer, we register the POST /user route, which will process the TestController::store() method, and we also define the TestMiddleware middleware for this route

In this case, to define middlewares, you must only pass an object of type Ghosty\Component\Routing\Contracts\Bags\MiddlewareBag

// routes/api.php
/**
* @var RouteStackContract
*/
$RouteStack = Container::make(RouteStackContract::class);
$RouteStack->push(new Route(
    "POST",
    "/user",
    \App\Controllers\TestController::class,
    "store",
    new ParameterBag(),
    new MiddlewareBag([TestMiddleware::class])
));

Returning Method

To register routes this way, you need to set the return parameter in the config/routing.php configuration file to true:

// config/routing.php
// If true routes file should return a RouteStack object.
// If false, the singleton RouteStackContract is used.
// default: false
'return' => true,

With this method of registering routes, the file with the description of the routes must return an object of the RouteStack class from which the routes will be loaded, for example:

// routes/api.php
return (new RouteStack)->push(new Route(
    "GET",
    "/user",
    \App\Controllers\TestController::class,
    "show"
));

Basics

In this example we define a route GET /user ServiceContainer this route will process the TestController::show() method.

// routes/api.php
return (new RouteStack)->push(new Route(
    "GET",
    "/user",
    \App\Controllers\TestController::class,
    "show"
));

Parameters

In this example, we use ServiceContainer to define a GET /user/{id} route that will process the TestController::show() method, where {id} is a dynamic parameter:

In this case, to define parameters, you must only pass an object of type Ghosty\Component\Routing\Contracts\Bags\ParameterBag

// routes/api.php
return (new RouteStack)->push(new Route(
    "GET",
    "/user/{id}",
    \App\Controllers\TestController::class,
    "show",
    new ParameterBag(['{id}'])
));

Middlewares

In this example, using the ServiceContainer, we register the POST /user route, which will process the TestController::store() method, and we also define the TestMiddleware middleware for this route

In this case, to define middlewares, you must only pass an object of type Ghosty\Component\Routing\Contracts\Bags\MiddlewareBag

// routes/api.php
return (new RouteStack)->push(new Route(
    "POST",
    "/user",
    \App\Controllers\TestController::class,
    "create",
    new ParameterBag(),
    new MiddlewareBag([TestMiddleware::class])
));