最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论