Laravel 4如何通过组名获取路由 [英] Laravel 4 how get routes by group name

查看:191
本文介绍了Laravel 4如何通过组名获取路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中,我知道我可以使用`Route :: getRoutes()获取所有路由,但是我找不到是否可以获取指定组中包含的所有路由的列表.

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.

例如,我有这个路由文件:

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 

在我的控制器中,我只想获取第一组(变量为"group_name"的一组)中包含的路由.

and in my controller i want obtain only routes contained in the first group (the one with the variable 'group_name').

有可能吗?如果可以,我该怎么办?谢谢

Is it possible? If yes how I can do it? Thanks

推荐答案

第一个参数中传递给组的属性存储在action数组中的路由上.可以通过路由上的getAction()方法访问此数组.因此,一旦访问路线对象,就可以基于此信息进行过滤.

The attributes passed to the group in the first parameter are stored on the route in the action array. This array can be accessed via the getAction() method on the route. 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);

这篇关于Laravel 4如何通过组名获取路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆