PHPackages                             roy404/routes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. roy404/routes

ActiveLibrary[HTTP &amp; Networking](/categories/http)

roy404/routes
=============

A routing library that provides an intuitive, object-oriented way to define and manage HTTP routes in your PHP application, supporting route grouping, middleware, and custom route handling.

4.4.5(4mo ago)32232MITPHPPHP ^8.0

Since Oct 14Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/roycanales17/PHP-Router)[ Packagist](https://packagist.org/packages/roy404/routes)[ RSS](/packages/roy404-routes/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (57)Used By (2)

ROUTES - HTTP Handler
=====================

[](#routes---http-handler)

Easily manage HTTP requests in your PHP application using the `roy404/routes` bundle.

Installation
------------

[](#installation)

Install the package using Composer:

```
composer require roy404/routes
```

---

Route Feature Overview
----------------------

[](#route-feature-overview)

The Route feature allows you to define and manage HTTP requests in a structured way. You can handle `GET`, `POST`, `PUT`, `PATCH`, `DELETE` requests, `group` routes, assign `middleware`, define `controllers`, and more.

---

Available Methods
-----------------

[](#available-methods)

### HTTP Methods

[](#http-methods)

- `Route::get(string $uri, string|array|Closure $action)` – Define a GET route.
- `Route::post(string $uri, string|array|Closure $action)` – Define a POST route.
- `Route::put(string $uri, string|array|Closure $action)` – Define a PUT route.
- `Route::patch(string $uri, string|array|Closure $action)` – Define a PATCH route.
- `Route::delete(string $uri, string|array|Closure $action)` – Define a DELETE route.

### Route Configuration

[](#route-configuration)

- `Route::group(array $attributes, Closure $action)` – Group routes with shared configuration or middleware.
- `Route::controller(string $className)` – Assign a controller for specific routes.
- `Route::middleware(string|array $action)` – Assign middleware to routes.
- `Route::prefix(string $prefix)` – Add a URI prefix for a group of routes.
- `Route::name(string $name)` – Assign a name to a route.
- `Route::domain(string|array $domain)` – Restrict a route to a specific domain.

---

Examples
--------

[](#examples)

### 1. Group Routes

[](#1-group-routes)

**Definition**
`Route::group()` allows you to group multiple routes under shared configuration such as middleware, prefixes, domains, or naming conventions.

This is useful for organizing routes and avoiding repetitive configuration.

```
Route::group(['middleware' => 'auth'], function () {
    Route::get('/dashboard', function () {
        echo 'Welcome to the Dashboard';
    });

    Route::get('/profile', function () {
        echo 'Your Profile';
    });
});
```

**How it works**

- All routes inside the group inherit the group’s attributes.
- Common use cases include authentication, admin panels, and API versioning.

### 2. Controller

[](#2-controller)

**Definition**
`Route::controller()` assigns a controller class to a group of routes. All routes within the group will automatically use the specified controller.

```
Route::controller(HomeController::class)->group(function () {
    Route::get('/home', 'index');
});
```

**How it works**

- The string 'index' refers to the `index()` method on HomeController.
- This keeps route files clean and moves logic into controllers.

### 3. Middleware

[](#3-middleware)

**Definition**
`Route::middleware()` allows you to attach one or more middleware to a route or route group. Middleware is executed before the route action and is commonly used for authentication, authorization, logging, or request validation.

```
Route::middleware([Auth::class, 'isAuthenticated'])->group(function () {
    Route::get('/profile', function () {
        echo 'Your profile';
    });
});
```

**Handling Unauthorized Requests**
If a middleware check fails, you may define an `unauthorized()` handler. This callback is executed when access is denied, allowing you to customize the response (e.g., redirect, return JSON, or show an error page).

If failed, you can use `unauthorized` method to handle it.

```
Route::middleware([Auth::class, 'isAuthenticated'])
    ->group(function () {
        Route::get('/profile', function () {
            echo 'Your profile';
        });
    })
    ->unauthorized(function () {
        // Handle unauthorized access here
        // Example: redirect to login page or return a 401 response
    });
```

**Key Notes**

- The unauthorized() callback is triggered when middleware validation fails.
- This is useful for centralized access control handling.
- Works seamlessly with route groups and individual routes.

**Route-Level Middleware**
Middleware can also be applied directly to a single route instead of an entire group. This is useful when only specific routes require protection or special processing.

```
Route::get('/profile', function () {
    echo 'Your profile';
})->middleware([Auth::class, 'isAuthenticated']);
```

**How it works**

- The middleware is executed before the route’s action.
- If the middleware validation passes, the route action runs.
- If it fails, the request is blocked and handled as unauthorized.

### 4. Prefix

[](#4-prefix)

**Definition**
`Route::prefix()` adds a URI prefix to all routes inside the group. This is commonly used for admin panels, APIs, or versioned routes.

```
Route::prefix('admin')->group(function () {
    Route::get('/dashboard', function () {
        echo 'Admin Dashboard';
    });
});
```

**Resulting URI**

```
/admin/dashboard
```

**How it works**

- The prefix is automatically prepended to every route in the group.
- Can be combined with middleware and domain rules.

### 5. Name

[](#5-name)

**Definition**
`Route::name()` assigns a name to a route or a group of routes. Named routes allow you to reference URLs without hardcoding paths.

```
Route::name('user')->group(function () {
    Route::get('home', function () {
        echo 'Your home';
    })->name('home');

    Route::get('profile', function () {
        echo 'Your profile';
    })->name('profile');
});
```

**Generated Route Names**

```
user.home
user.profile
```

**How it works**

- Group names act as a prefix.
- Route names are useful for URL generation and refactoring.

### 6. Domain

[](#6-domain)

**Definition**
`Route::domain()` restricts a group of routes to a specific domain or subdomain. This is useful for multi-tenant, admin, or API-based architectures.

```
Route::domain('admin.example.com')->group(function () {
    Route::get('/home', function () {
        echo 'Your home';
    });
});
```

**How it works**

- Routes inside the group will only respond to the specified domain.
- Can be combined with prefixes and middleware.
- Ideal for separating admin and public interfaces.

---

Getting Started
---------------

[](#getting-started)

This section walks you through setting up the routing system and defining your first routes.

### 1. Configure Routes

[](#1-configure-routes)

The `Route::configure()` method initializes the routing system. It tells the router where your project lives, which route files to load, and optionally applies global configuration such as prefixes, domains, and middleware.

```
