Welcome Guest, Not a member yet? Register   Sign In
(SOLVED) Grouping Resource or Presenter routes
#1

(This post was last modified: 08-06-2023, 07:40 AM by sammyskills.)

Hi,
I recently came across a situation where I want to group some routes in this format:
Code:
/users
/users/groups
/users/permissions

Now, the idea is that all the routes are presenter routes and I've tried to group them like so: 
Code:
$routes->group('users', function($routes) {
    $routes->presenter('/');
    $routes->presenter('groups');
    $routes->presenter('permissions');
});

So far, the route for the users ends up with double slashes. Any idea on how best to implement something like this?
Reply
#2

Try this:

PHP Code:
$routes->presenter('users');
$routes->group('users', function($routes) {
    $routes->presenter('groups');
    $routes->presenter('permissions');
}); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

Very helpful for me
Reply
#4

(11-19-2021, 01:50 PM)includebeer Wrote: Try this:

PHP Code:
$routes->presenter('users');
$routes->group('users', function($routes) {
    $routes->presenter('groups');
    $routes->presenter('permissions');
}); 

Thanks for your suggestion. I tried this earlier while trying to get a workaround. It worked, but with issues.

The $route->presenter('groups') and $route->presenter('permissions') routes are being treated as child of the $route->presenter('users'). So when you browse to /users/groups or /users/permissions, you get a blank page, no errors. 

It is assumed that the resource controller would have a method named groups and permissions
Reply
#5

What routes do you have if you run "php spark routes"? I don't understand why it would define a route calling a groups() of a permissions() method. Do you have an index() method in your controllers? See https://codeigniter.com/user_guide/incom...ter-routes
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#6

When I run php spark routes, I get a list of all the routes, pointing to their respective controllers, in the way I expected:

Quote:users
users/groups
users/permissions

But the issue still remains that if I try to visit the routes users/groups or users/permissions, the page goes blank. No errors at all.
This can only mean that they are being treated as child routes of the resource presenter, and that the route expects a groups() and permissions() methods in the controller definition. 

I've tried adding the methods manually in the users controller, but, not working, still showing a blank page.
Reply
#7

Why would it expect a groups() and permissions() method? If you have autoroute enabled, disable it. Using both autoroute and custom routes will just lead to weird and unwanted behavior. You say that "php spark routes" display the list of routes you expect but it doesn't call the method it is supposed? Do you have an index() method or not?

I think it may be because the order I told you is wrong. When you go to users/groups it probably go to this route:
PHP Code:
$routes->get('users/(:segment)''Users::show/$1'); 

The users routes should probably be defined last. Try it like this:
PHP Code:
$routes->group('users', function($routes) {
    $routes->presenter('groups');
    $routes->presenter('permissions');
});
$routes->presenter('users'); 

If this doesn't work, maybe define simpler routes like this?
PHP Code:
$routes->presenter('users');
$routes->presenter('userGroups');
$routes->presenter('userPermissions'); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#8

Yes, the order was the issue. I had already settled for this just to get it working, while hoping for a better solution:
PHP Code:
$routes->presenter('users');
$routes->presenter('user-groups');
$routes->presenter('user-permissions'); 

Amazing, I just tried re-ordering just as you suggested and it worked!

Thanks a lot!
Reply
#9

Please Try This Code Hope Its Fine for You Thanks

PHP Code:
$routes->presenter('users');
$routes->group('users', function($routes) {
    $routes->presenter('groups');
    $routes->presenter('permissions');
});
Reply




Theme © iAndrew 2016 - Forum software by © MyBB