Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create route through an iterator #28418

Closed
wants to merge 3 commits into from
Closed

Create route through an iterator #28418

wants to merge 3 commits into from

Conversation

aronpc
Copy link

@aronpc aronpc commented May 5, 2019

Create a route group from an iterator.

So we can separate our routes into more files avoiding that they get too big:

./routes
├── api.php
├── api.posts.php
├── api.users.php
├── channels.php
├── web.php
├── web.posts.php
└── web.users.php

    protected function mapWebRoutes()
    {
        $directory = new \RecursiveDirectoryIterator(base_path('routes/'));
        $files = new \RegexIterator($directory, '/web(.*).php$/');

        Route::middleware('web')
            ->namespace($this->namespace)
            ->group($files);
    }

All the tests in the framework are passing and did not have any major modifications.

@taylorotwell
Copy link
Member

I suggest just registering the files manually. These types of file operations can be slow.

@aronpc
Copy link
Author

aronpc commented May 5, 2019

We can use the same code without the RecursiveDirectoryIterator, so we can load the routes.

This would make it easier to load more files in the same namespace...
:)

    protected function mapWebRoutes()
    {
        $routes = new ArrayIterator( [
                base_path('routes/web.php'),
                base_path('routes/web.users.php'),
                ...
        ] );

        Route::middleware('web')
            ->namespace($this->namespace)
            ->group($routes);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants