PHP code example of projecthanif / routescope

1. Go to this page and download the library: Download projecthanif/routescope library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

projecthanif / routescope example snippets


return [
    // Only enable in local/development environments
    'enabled' => env('ROUTESCOPE_ENABLED', app()->environment('local', 'development')),
    
    // Customize the dashboard URL
    'prefix' => env('ROUTESCOPE_PREFIX', 'routescope'),
    
    // Hide routes you don't want to see (debug tools, internal routes, etc.)
    'excluded_patterns' => [
        'routescope',
        '_ignition',
        'sanctum/csrf-cookie',
        'telescope',
        'horizon',
    ],
];

use Projecthanif\RouteScope\Facades\RouteScope;

$routes = RouteScope::getAllRoutes();

// Returns:
[
    'apiRoutes' => [...],  // All /api/* routes
    'webRoutes' => [...]   // All other routes
]

use Projecthanif\RouteScope\Facades\RouteScope;

class InspectRoutes extends Command
{
    public function handle()
    {
        $all = RouteScope::getAllRoutes();
        
        $this->info('API Routes:');
        foreach ($all['apiRoutes'] as $route) {
            $this->line("  {$route['method']} {$route['path']}");
        }
        
        $this->info('Web Routes:');
        foreach ($all['webRoutes'] as $route) {
            $this->line("  {$route['method']} {$route['path']}");
        }
    }
}

use Projecthanif\RouteScope\Services\RouteScopeService;

$service = app(RouteScopeService::class);
$routes = $service->getAllRoutes();

$authRoutes = collect($routes['webRoutes'])
    ->filter(fn($route) => in_array('auth', $route['middleware']))
    ->all();

use Projecthanif\RouteScope\Facades\RouteScope;

$routes = RouteScope::getAllRoutes();

$markdown = "# API Endpoints\n\n";

foreach ($routes['apiRoutes'] as $route) {
    $markdown .= "### {$route['method']} {$route['path']}\n\n";
    $markdown .= "**Controller**: `{$route['source']}`\n";
    $markdown .= "**Middleware**: " . implode(', ', $route['middleware']) . "\n\n";
}

file_put_contents('api-docs.md', $markdown);

use Projecthanif\RouteScope\Services\RouteScopeService;

class RouteAnalysisController extends Controller
{
    public function __construct(
        private RouteScopeService $routeScope
    ) {}
    
    public function index()
    {
        $routes = $this->routeScope->getAllRoutes();
        
        return view('admin.routes', compact('routes'));
    }
}

[
    'apiRoutes' => [
        [
            'method' => 'GET|POST|PUT|DELETE|PATCH',
            'path' => '/api/users',
            'name' => 'users.index',  // or null if unnamed
            'source' => 'App\Http\Controllers\UserController::index',
            'middleware' => ['api', 'auth:sanctum'],
        ],
        // ... more routes
    ],
    'webRoutes' => [
        [
            'method' => 'GET',
            'path' => '/dashboard',
            'name' => 'dashboard',
            'source' => 'App\Http\Controllers\DashboardController::show',
            'middleware' => ['web', 'auth'],
        ],
        // ... more routes
    ]
]
bash
php artisan vendor:publish --tag=routescope-config