admin管理员组文章数量:1390265
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
1 Answer
Reset to default 1There 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.
本文标签: phphow to access codeignter4 routes metadata by route nameStack Overflow
版权声明:本文标题:php - how to access codeignter4 routes metadata by route name? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744723946a2621860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论