Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在laravel 5中按特定组获取路线列表?_Laravel_Laravel 5 - Fatal编程技术网

如何在laravel 5中按特定组获取路线列表?

如何在laravel 5中按特定组获取路线列表?,laravel,laravel-5,Laravel,Laravel 5,你好,我正试图这样做,但它得到了所有的路线,我只想从一个特定的组(s)的路线 这是我的代码: <?php $routes = Routes::getRoutes(); @foreach($routes as $route) {{ $route->getPath() }} @endforeach` 让我们创建一些没有任何组的路由 Route::get('/', function () { return view('welcome'); }); Route::get

你好,我正试图这样做,但它得到了所有的路线,我只想从一个特定的组(s)的路线

这是我的代码:

<?php 
$routes = Routes::getRoutes();
@foreach($routes as $route)
    {{ $route->getPath() }}
@endforeach`

让我们创建一些没有任何组的路由

Route::get('/', function () {
    return view('welcome');
});

Route::get('/load', 'defaultController@load');
Route::group(['as' => 'admin'], function () {
    Route::get('users', function ()    {
        return "users route";
    });

    Route::get('ravi', function ()    {
        return "ravi route";
    });
现在我们将使用组创建一些路由

Route::get('/', function () {
    return view('welcome');
});

Route::get('/load', 'defaultController@load');
Route::group(['as' => 'admin'], function () {
    Route::get('users', function ()    {
        return "users route";
    });

    Route::get('ravi', function ()    {
        return "ravi route";
    });
现在我们将在此组中创建一个路由,该路由将查找管理组并打印此组中存在的所有路由。

Route::get('kumar', function ()    {
    $name = 'admin';
    $routeCollection = Route::getRoutes(); // RouteCollection object
    $routes = $routeCollection->getRoutes(); // array of route objects
 $grouped_routes = array_filter($routes, function($route) use ($name) {
            $action = $route->getAction(); // getting route action
            if (isset($action['as'])) {

                // for the first level groups, $action['as'] 
                // will be a string
                // for nested groups, $action['as'] will be an array

                if (is_array($action['as'])) {
                    return in_array($name, $action['as']);
                } else {
                    return $action['as'] == $name;
                }
            }
            return false;
        });


  // Here we will print the array containing the route objects in the 'admin' group
        dd($grouped_routes);
    });

});
现在在route对象中,我们将通过过滤数组来查找命名路由。

Route::get('kumar', function ()    {
    $name = 'admin';
    $routeCollection = Route::getRoutes(); // RouteCollection object
    $routes = $routeCollection->getRoutes(); // array of route objects
 $grouped_routes = array_filter($routes, function($route) use ($name) {
            $action = $route->getAction(); // getting route action
            if (isset($action['as'])) {

                // for the first level groups, $action['as'] 
                // will be a string
                // for nested groups, $action['as'] will be an array

                if (is_array($action['as'])) {
                    return in_array($name, $action['as']);
                } else {
                    return $action['as'] == $name;
                }
            }
            return false;
        });


  // Here we will print the array containing the route objects in the 'admin' group
        dd($grouped_routes);
    });

});
现在,您可以将其复制并粘贴到route文件夹中,点击您的\u project\u public\u文件夹\u url/kumar即可查看输出


我从这个答案中获得了帮助

请给出一个示例例如:我的表中有一个名为permissions的列,我将在其中存储一些与特定路由组相关的特定路由。