php - how to access codeignter4 routes metadata by route name? - Stack Overflow

i have routes,and nested groups of routes.some routes have metadata contain required permissions or rol

i have routes,and nested groups of routes.some routes have metadata contain required permissions or rolles ,which are checked against user session variables. i handled the check in controller constructor by using service('router')->getMatchedRouteOptions() and check with helper function which throw exception 404 if session()->get('user_role') not in allowed array

but the problem is in some nested views and components, which have list of links to these routes ,which should be also conditionally listed according to the given permissions in routes metadata

1- how can i access the metadata by route name in any part of the application using a function like: isAllowed($route_name) which check if route has metadata allowed->roles , intersecting with with user permissions or roles?

2- how can i get routes under specific group by group name givin also in metdata alias 'as'?

i want the permissions in metadata to be centralized , without creating another soulution which need duplicating routes

below is a sample of the route

$routes->group('users', ['as' => 'admin_users'], static function ($routes) {
    // Route for the users main page
    $routes->get('', 'AdminControllers\ViewController::users_main_page', [
        'as' => 'admin_master_page',
        'allowed' => [
            'role' => ['admin', 'super_admin', 'owner'],
        ]
    ]);

    // Route for the users search page
    $routes->get('search', 'AdminControllers\ViewController::users_search', [
        'as' => 'user_search',
        'allowed' => [
            'role' => ['super_admin', 'owner'],
        ]
    ]);
    $routes->get('add', 'AdminControllers\ViewController::add_user', [
        'as' => 'add_user',
        'allowed' => [
            'role' => ['super_admin', 'owner'],
        ]
    ]);

});

i have routes,and nested groups of routes.some routes have metadata contain required permissions or rolles ,which are checked against user session variables. i handled the check in controller constructor by using service('router')->getMatchedRouteOptions() and check with helper function which throw exception 404 if session()->get('user_role') not in allowed array

but the problem is in some nested views and components, which have list of links to these routes ,which should be also conditionally listed according to the given permissions in routes metadata

1- how can i access the metadata by route name in any part of the application using a function like: isAllowed($route_name) which check if route has metadata allowed->roles , intersecting with with user permissions or roles?

2- how can i get routes under specific group by group name givin also in metdata alias 'as'?

i want the permissions in metadata to be centralized , without creating another soulution which need duplicating routes

below is a sample of the route

$routes->group('users', ['as' => 'admin_users'], static function ($routes) {
    // Route for the users main page
    $routes->get('', 'AdminControllers\ViewController::users_main_page', [
        'as' => 'admin_master_page',
        'allowed' => [
            'role' => ['admin', 'super_admin', 'owner'],
        ]
    ]);

    // Route for the users search page
    $routes->get('search', 'AdminControllers\ViewController::users_search', [
        'as' => 'user_search',
        'allowed' => [
            'role' => ['super_admin', 'owner'],
        ]
    ]);
    $routes->get('add', 'AdminControllers\ViewController::add_user', [
        'as' => 'add_user',
        'allowed' => [
            'role' => ['super_admin', 'owner'],
        ]
    ]);

});
Share Improve this question asked Mar 13 at 1:07 Black DevilBlack Devil 255 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There is no built-in function. You need to create the handler yourself. In any case, you need an extended class - there are no public methods for searching for a router by name.

There is an example with routes in the documentation: https://codeigniter4.github.io/userguide/concepts/services.html#defining-services

Add new methods to your class and use them anywhere as a service('routes')->isAllow('homepage').

I think there is no answer to the second question. CodeIgniter does not store the names of the group as you have defined it. He combines the options in the process and fets about it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744723947a4590074.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信