DEV Community

Lloric Mayuga Garcia
Lloric Mayuga Garcia

Posted on

Adding functionality to add entire folder to a route in Laravel

    // App\Providers\RouteServiceProvider

    public function map()
    {
        Route::macro('requireFolder', function (string $folder) {
            try {

                $it = new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($folder)
                );

                while ($it->valid()) {
                    if (
                        !$it->isDot() &&
                        $it->isFile() &&
                        $it->isReadable() &&
                        $it->current()->getExtension() === 'php'
                    ) {
                        require $it->key();
                    }

                    $it->next();
                }
            } catch (Exception $e) {
                dd(__METHOD__, $e->getMessage());
            }
        });

        $this->mapWebApiRoutes();
        $this->mapWebRoutes();
    }
Enter fullscreen mode Exit fullscreen mode

now you can just use this

Route::group([
    'prefix' => 'admin',
], function()
{
    Route::requireFolder(__DIR__.'/backend');
});

Enter fullscreen mode Exit fullscreen mode

all php files will automagically require

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay