jeudi 13 septembre 2018

Generate nested UL/LI from laravel route collection

I am attempting to generate a dynamic sitemap for my laravel project.

I'd like to iterate through a route collection and generate a nested UL/LI structure for all the routes.

Currently I am doing the following which 'kind' of works:

Controller:

$routeCollection = Route::getRoutes();
$mainRoutes = [];
$allRoutes = [];
foreach ($routeCollection as $value) {
    if (!in_array($value->uri(), $allRoutes)) {
        array_push($allRoutes, $value->uri());
    }
    $url = explode('/', $value->uri());
    if (!in_array($url[0], $mainRoutes)) {
        array_push($mainRoutes, $url[0]);
    }
}

Blade:

<ul>
    @foreach($mainRoutes as $main_route)
    <li>
        <ul>
        <?php
            foreach ($allRoutes as $route)
            {
                $url = explode('/', $route);
                if ($main_route == $url[0])
                {
            ?><li></li><?php
                }
            }
        ?>
        </ul>
    </li>
    @endforeach
</ul>

The problem here is I am assuming a depth of just one ($mainRoutes) so nested paths just appear under the first nested UL.

Ideally the output would be something like:

URL's:

//foo 
//foo/bar
//foo/biz/bang
//foo/biz/bing
//foo/bee/beep/bop
//foo/bee/beep/blop

List:

-Foo
--Bar
--Biz
---Bang
---Bing
--Bee
---Beep
----Bop
----Blop

...and be able to handle any level of nesting.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2QmYzLz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire