DEV Community

loading...
Cover image for Auto Route Generator for Laravel

Auto Route Generator for Laravel

İzni Burak Demirtaş
Software Engineer — #php #java #golang #javascript #react-native— Co-Founder at HaberSistemim
・2 min read

While working on a project with Laravel, I thought that “I wish all these routes were created automatically”. Then, I’ve started to research a package about that but I couldn’t find a package I want.

I’ve decided to make a simple package for that. I’ve made some research about how can I create a new Laravel package because I’d not created any package for Laravel before. I’ve been using Laravel about 5 years, but this would be my first package for Laravel.

With this package; you will be able to generate routes automatically and configure some options about the Package by using its config file. Now, you can check the example for the Package below.

Firstly, you must install the package via the Composer command:

composer require izniburak/laravel-auto-routes
Enter fullscreen mode Exit fullscreen mode

After that, you should add a new route definition by using auto method that will come from laravel-auto-routes package.

# in routes/web.php or routes/api.php 
Route::auto('/test', TestController::class);
Enter fullscreen mode Exit fullscreen mode

Just route prefix and the Controller file are enough for definition. If you want to change some options like namespace, middleware or name, you can use the third parameter of the auto method.

After that, you can add a new controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL will be converted to "/test/foo-bar"
     */
    public function fooBar(Request $request)
    {
        // your codes
    }
   /**
    * URL will be converted to "/test/bar-baz"
    */
    public function barBaz(Request $request)
    {
        // your codes
    }
}
Enter fullscreen mode Exit fullscreen mode

That’s all. All methods in your Controller will be generated to compatible endpoints automatically.

You can use parameters, HTTP methods, and more in your Controller. If you want to learn more details about the package, you can check the Github page:

Github: laravel-auto-routes
Auto Route Generating Package for Laravel.

I hope this package would be useful to everyone and help them. If you have any suggestions or ideas about the package, Please feel free to give them. I’d love to hear your feedback.

Thanks,
İzni Burak.

Discussion (1)

Collapse
mohsinaliryk profile image
Mohsin Ali

Well done I will definitely test it.