简体   繁体   English

Laravel 4如何通过组名获取路由

[英]Laravel 4 how get routes by group name

In Laravel, I know I can get all routes using `Route::getRoutes() but I can't find if it is possible to get a list of all routes contained in a specified group. 在Laravel中,我知道我可以使用`Route :: getRoutes()获取所有路由,但是我找不到是否可以获取指定组中包含的所有路由的列表。

For example, i have this route file: 例如,我有此路由文件:

Route::group(array('group_name' => 'pages'), function() {
    Route::any('/authentication', array('as' => 'authentication', 'uses' => 'LogController@authForm' ));
    Route::group(array('before' => 'auth_administration'), function() {
        Route::any('/tags_category/index', array('as' => 'index-tags-categories', 'uses' => 'TagsCategoryController@index'));
        Route::any('/tags_category/update', array('as' => 'update-tags-category', 'uses' => 'TagsCategoryController@update'));
    });
});

Route::group(array('before' => 'auth_administration'), function() {
    Route::any('/tags_category/store', array('as' => 'store-tags-category', 'uses' => 'TagsCategoryController@store')); 
    Route::any('/tags_category/update/{id}', array('as' => 'update-form-tags-category', 'uses' => 'TagsCategoryController@updateForm')); 
    Route::any('/tags_category/delete/{id}', array('as' => 'delete-tags-category', 'uses' => 'TagsCategoryController@delete'));
}); // operazioni protette 

and in my controller i want obtain only routes contained in the first group (the one with the variable 'group_name'). 在我的控制器中,我只想获取包含在第一组(变量为“ group_name”的一组)中的路由。

Is it possible? 可能吗? If yes how I can do it? 如果可以,我该怎么办? Thanks 谢谢

The attributes passed to the group in the first parameter are stored on the route in the action array. 在第一个参数中传递给组的属性存储在action数组中的路由上。 This array can be accessed via the getAction() method on the route. 可以通过路由上的getAction()方法访问此数组。 So, once you get access to the route objects, you can filter based on this information. 因此,一旦访问路线对象,就可以基于此信息进行过滤。

$name = 'pages';
$routeCollection = Route::getRoutes(); // RouteCollection object
$routes = $routeCollection->getRoutes(); // array of route objects
$grouped_routes = array_filter($routes, function($route) use ($name) {
    $action = $route->getAction();
    if (isset($action['group_name'])) {
        // for the first level groups, $action['group_name'] will be a string
        // for nested groups, $action['group_name'] will be an array
        if (is_array($action['group_name'])) {
            return in_array($name, $action['group_name']);
        } else {
            return $action['group_name'] == $name;
        }
    }
    return false;
});

// array containing the route objects in the 'pages' group
dd($grouped_routes);

User artisan to list all routes for the application 技术人员可以列出应用程序的所有路线

php artisan routes --name=admin

in Laravel 5 在Laravel 5中

php artisan route:list --name=admin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM