Skip to content

Added custom web routes documentation #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ <h5 class="offcanvas-title" id="offcanvasNavbarLabel">Navigation</h5>
<a href="?page=developing-extensions/Admin-configuration"><button type="button" class="btn btn-sm text-start docs-nav" style="--bs-btn-padding-y: .1rem;">Admin configuration</button><br></a>
<a href="?page=developing-extensions/Dashboard-wrappers"><button type="button" class="btn btn-sm text-start docs-nav" style="--bs-btn-padding-y: .1rem;">Dashboard wrappers</button><br></a>
<a href="?page=developing-extensions/React-components"><button type="button" class="btn btn-sm text-start docs-nav" style="--bs-btn-padding-y: .1rem;">React components</button><br></a>
<a href="?page=developing-extensions/Custom-web-routes"><button type="button" class="btn btn-sm text-start docs-nav" style="--bs-btn-padding-y: .1rem;">Custom web routes</button><br></a>
<a href="?page=developing-extensions/Packaging-extensions"><button type="button" class="btn btn-sm text-start docs-nav" style="--bs-btn-padding-y: .1rem;">Packaging extensions</button><br></a>
</div>
<div class="docs-category pb-4">
Expand Down
104 changes: 104 additions & 0 deletions docs/pages/developing-extensions/Custom-web-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<div class="position-relative p-4 text-body bg-body border rounded-4 d-flex align-items-center">
<div class="me-3">
<i class="bi bi-book h2"></i>
</div>
<p class="me-3 my-0">
Written by those who've walked the path. Want to improve our guides? Contribute and help build something awesome!
</p>
<a href="https://github.com/BlueprintFramework/web/tree/main/docs/pages/developing-extensions">
<button class="btn btn-primary px-4 rounded-pill placeholder-wave" type="button">
Contribute
</button>
</a>
</div><br>

# Creating custom web routes
<h4 class="fw-light">Add your own web routes accessible from everywhere within blueprint.</h4><br/>

This guide provides a step-by-step overview on how to define custom web routes using a dedicated controller within a Blueprint extension.<br/><br/>

### **Define the controller directory and routing file**

Begin by creating a new directory within your extension to store your controller classes. In this example, we will use a directory named `controllers`.

Next, open your `conf.yml` file and define the location of your controller directory and route file under the `requests` section. This allows Blueprint to recognize and load your custom logic.

```yml
requests:
views: ""
app: "controllers"
routers:
application: ""
client: ""
web: "routes.php"
```

- `app`: Points to the folder containing your controller classes.
- `web`: Defines the file responsible for registering your web routes.

<div class="p-2 border-start border-4 mb-5">
<i class="bi bi-journal-text text-primary me-1"></i>
For more details on configuration, refer to the <a href="?page=documentation/confyml">conf.yml documentation</a>.
</div>

### **Creating a controller**

Inside the `controllers` directory, create a PHP file named after your controller class. The file and class name **must match exactly**.

In this example, we will create a controller that handles routes for the dashboard and name the file `ExtensionDashboardController.php`.

```php
<?php

namespace Pterodactyl\BlueprintFramework\Extensions\{identifier};

use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Admin\BlueprintAdminLibrary as BlueprintExtensionLibrary;

class ExtensionDashboardController extends Controller
{
public function __construct(
private BlueprintExtensionLibrary $blueprint,
) {}

public function getData()
{
return response()->json([
'status' => 'success',
]);
}
}
```
- This controller defines a `getData()` method that returns a JSON response.

<div class="p-2 border-start border-4 mb-5">
<i class="bi bi-globe text-primary me-1"></i>
For advanced controller techniques, see the <a href="https://laravel.com/docs/10.x/controllers">Laravel controller documentation</a>.
<br/>
<i class="bi bi-globe text-primary me-1"></i>
For different response types (redirects, files, etc.), refer to <a href="https://laravel.com/docs/10.x/responses">Laravel HTTP responses documentation</a>.
</div>

### **Registering routes**

Create a PHP file named `routes.php` (or another name, as long as it matches your `conf.yml`) to define your routes.

```php
<?php

use Illuminate\Support\Facades\Route;
use Pterodactyl\BlueprintFramework\Extensions\{identifier}\ExtensionDashboardController;

Route::get('/data', [ExtensionDashboardController::class, 'getData']);
```

- All routes registered in this file are automatically prefixed with `/extensions/{identifier}`.
- This example registers a new GET route accessible at `/extensions/{identifier}/data` and connects it to the `getData` function from `ExtensionDashboardController`.

<div class="p-2 border-start border-4 mb-5">
<i class="bi bi-exclamation-diamond text-warning me-1"></i>
Be sure to update the second <code>use</code> statement to match the correct namespace and class name of your controller.
<br/>
<i class="bi bi-globe text-primary me-1"></i>
For detailed route configuration, visit the <a href="https://laravel.com/docs/10.x/routing">Laravel routing documentation</a>.
</div>