İzni Burak Demirtaş introduced a Laravel package Auto Routes that generates routes from a controller using auto-discovery. It works by identifying public methods on a controller and making routes for them.
Installation
For the installation, you’ve to run this command:
composer require izniburak/laravel-auto-routes
Next, you’ve to publish the config file via the following command:
php artisan vendor:publish --provider="Buki\AutoRoute\AutoRouteServiceProvider"
Usage
You’ve to append a new route in the web.php or api.php files in the routes directory.
Route::auto('/test', 'TestController');
OR
Route::auto('/test', TestController::class);
All methods of this controller will generate automatically.
You can stipulate HTTP Method for the method of the Controllers. If you want that a method works with the GET method and another method to works with the POST method, you’ve to add a prefix for the method. For example;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
/**
* URL: "/test/foo-bar"
* This method will only work with 'GET' method.
*/
public function getFooBar(Request $request)
{
// your codes
}
/**
* URL: "/test/bar-baz"
* This method will only work with 'POST'
method.
*/
public function postBarBaz(Request $request)
{
// your codes
}
}
If you desire to use snake_case format for your methods, Here is the example to do this.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
/**
* URL: "/test/foo_bar"
* This method will only work with 'GET' method.
*/
public function get_foo_bar(Request $request)
{
// your codes
}
/**
* URL: "/test/bar_baz"
* This method will only work with 'POST' method.
*/
public function post_bar_baz(Request $request)
{
// your codes
}
}
Additionally, you can utilize patterns in the Options array to configure new patterns for the parameters of the methods in the Controllers. For example:
Route::auto('/test', 'TestController', [
'name' => 'test',
'middleware' => [YourMiddleware::class],
'patterns' => [
'id' => '\d+',
'value' => '\w+',
],
]);
For more details, you can view its full documentation here.
izniburak/laravel-auto-routes