Laravel Packages

Laravel Auto Routes Package

Published
Laravel Auto Routes Package image

Auto Routes for Laravel is a package by İzni Burak Demirtaş that generates routes from a controller using auto-discovery. It works by discovering public methods on a controller and generating routes for them:

// routes/web.php
Route::auto('/test', TestController::class);
 
//
// Controller example
//
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class TestController extends Controller
{
/**
* URL: "/test" - main method
*/
public function index(Request $request)
{
// controller code
}
 
/**
* URL: "/test/foo-bar"
* This method will only work with 'GET' method.
*/
public function getFooBar(Request $request)
{
// controller code
}
 
/**
* URL: "/test/foo_bar"
* This method will only work with 'GET' method.
*/
public function get_foo_bar(Request $request)
{
// controller code
}
 
 
/**
* URL: "/test/bar-baz"
* This method will only work with 'POST' method.
*/
public function postBarBaz(Request $request)
{
// controller code
}
}

The route method also takes a third argument to configure parameters and middleware:

Route::auto('/test', TestController::class, [
'name' => 'test',
'middleware' => [YourMiddleware::class],
// Parameters become available to methods
'patterns' => [
'id' => '\d+',
'value' => '\w+',
],
]);
 
// Limit the methods that will define routes
Route::auto('/foo', 'FooController', [
'only' => ['fooBar', 'postUpdatePost'],
]);

While most developers are happy with Laravel's built-in routing definitions (Laravel routing is so good in my humble option), it is a unique idea with examples in the source code of working with PHP's reflection API and the Laravel router. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article