Jump to content

URL Segments Not Working


Liam88
 Share

Recommended Posts

Hello,

Just wondering if anyone has come across URL segments not working.

I have the login register module and creating a dedicated user area for a better user experience.

So I am using website.com/profile/user-id/ as the user profile page.

This is set to only show for those with a login-register user role and access is all done through PW.

Here is the basic snip to just do something if user id in url or not and with or without role.

$userId = $input->urlSegment1;

if(ctype_digit($userId)) {
  // display profile of user matching id in urlSegment1
  $u = $users->get((int) $userId);
  if($u->id && $u->hasRole('login-register')) {
    echo "<h1>User $user->name info:</h1>";
    // output any fields from user
  } else{
    echo '<h1>Unknown or unavailable user</h1>';
  }
} else if($userId) {
  wire404(); // something other than user ID in urlSegment1

} else {
  // no urlSegment1 present
  // display links for user profiles?
  foreach($users->get('roles=login-register, sort=name') as $u) {
    echo "<li><a href='$u->id/'>$u->name</a></li>";
  }
}

However, all the test profiles I have created that have 'login-register' roles just get 404. If I change it to only show for 'superuser' then I can get mine to show.

Here is an example.

 

 

You will see when logged in it shows within the /login-register/ area. Eventually, it would redirect to /profile/user-id/ but kept it like this to show that user is logged in but 404 on profile. The screenshots below confirm set up is correct.

Any direction on this is appreciated. I'm stuck on why this would be happening. It's like it's not recognising the role. I have added a handful of users to test between behaviours with none working. 

 

image.png.5207b98c94d29aed89f9756c290c78ed.png

image.png.705682e510e655bf3b4832dbd8792ad7.png

 

Link to comment
Share on other sites

Hello @Liam88,

do you mean the free Login Register module or the commercial Login Register Pro module?

I have no experience with the commercial module but I think neither require URL segments.

You either just let the module do its default behavior:

<?= $modules->get('LoginRegister')->execute() ?>

Or you customize it:

<?php
if($user->isLoggedin() && !$input->get('profile') && !$input->get('logout')) {
  // you take over and render your own output or redirect elsewhere
  $session->redirect('/some/other/page/'); 
} else {
  // let the LoginRegister module have control
  echo $modules->get('LoginRegister')->execute(); 
}

Source

Regards, Andreas

Link to comment
Share on other sites

22 minutes ago, AndZyk said:

Hello @Liam88,

do you mean the free Login Register module or the commercial Login Register Pro module?

I have no experience with the commercial module but I think neither require URL segments.

You either just let the module do its default behavior:

<?= $modules->get('LoginRegister')->execute() ?>

Or you customize it:

<?php
if($user->isLoggedin() && !$input->get('profile') && !$input->get('logout')) {
  // you take over and render your own output or redirect elsewhere
  $session->redirect('/some/other/page/'); 
} else {
  // let the LoginRegister module have control
  echo $modules->get('LoginRegister')->execute(); 
}

Source

Regards, Andreas

Hey @AndZyk

Thank you for your reply. 

I use the pro module.module.

I'm not looking to use the login-register/profile? areas but just use it for user management. So that would be create account, change password etc.

I then have a dedicated user area under /profile/ which does all the custom bits for users. Think Pinterest style where you have an area that shows posts and boards. 

So in that /profile/ I then use URL segments to grab the relevant posts and boards linked to their user id. So /profile/user-id/

So the snip I inserted above takes URL seg 1 and then would action based on the behaviour I dictate.

The template settings are all set up to do this so I shouldn't need bits like is logged in etc. So access management all done via PW. 

The screenrecord is just to show that I'm logged in and have a login-register role but get a 404 when viewing the /profile/ page. 

hope that helps explain better on why and the intention.

Link to comment
Share on other sites

Thank you for explanation.

This is working for me:

<?php
if ($input->urlSegment1) {
    $userID = $sanitizer->int($input->urlSegment1());
    $u = $users->get($userID);

    if ($u->id) {
        if ($u->hasRole("login-register")) {
            echo $u->name;
            echo $u->email;
        } else {
            wire404();
        }
    } else {
        wire404();
    }
}

If you always get a 404 error, then maybe your ctype_digit check fails.

Regards, Andreas

  • Like 1
Link to comment
Share on other sites

7 minutes ago, AndZyk said:

Thank you for explanation.

This is working for me:

<?php
if ($input->urlSegment1) {
    $userID = $sanitizer->int($input->urlSegment1());
    $u = $users->get($userID);

    if ($u->id) {
        if ($u->hasRole("login-register")) {
            echo $u->name;
            echo $u->email;
        } else {
            wire404();
        }
    } else {
        wire404();
    }
}

If you always get a 404 error, then maybe your ctype_digit check fails.

Regards, Andreas

Thanks @AndZyk

Trying this snip is also firing a 404. 

If I remove access management set in the template then I can get it to work. Taking your snip as an example I get this.

image.thumb.png.2c8611600bc984c55bf9f6399cea7fdc.png

It would just mean I would have to do access management within the PHP (e.g $u->isLoggedIn()) and not leave PW to it.

However, that feels against the reasons for it being within PW.

So the summarise, it seems this is an access issue and not a recognising role issue. Unless it's the access control not recognising the role. Who knows haha.

  • Like 2
Link to comment
Share on other sites

2 hours ago, Liam88 said:

However, that feels against the reasons for it being within PW.

So the summarise, it seems this is an access issue and not a recognising role issue. Unless it's the access control not recognising the role. Who knows haha.

Yeah your approach seems reasonable, would love to know if you manage to solve with with just the access control. I had always used the isLoggedin() method, and never ocurred to me that it'd be reasonable to just disable 'view' for guests in a specific template. I mean, if it can actually work like that haha.

Link to comment
Share on other sites

try to use $page->viewable() instead of asking for the user role.. and report back to us

Note: if you want the logged user to only be able to check his own profile, you must add this check also, comparing the ID from the urlSegment with the ID of the current user

Link to comment
Share on other sites

Confirming that your snippet works fine here with LoginRegisterPro.

482676272_Screenshot2022-09-09200302.png.7e715067bcb33a64f3af510ea9ebd824.png

 

But @Liam88 you have an issue, and if you are trying to debug/navigate to the url given by your $u->id link when there is no urlSegments, then you will end up on wrong user id, and then certainly on a random page ?

// no urlSegment1 present
// display links for user profiles?
foreach($users->get('roles=login-register, sort=name') as $u) {
  echo "<li><a href='$u->id/'>$u->name</a></li>";
}

$users->get() return a Page or null, so you are only getting iteration on the page's properties. You should use $users->find() to get the right $u page and his real id.

Try with the correction and report back.

And just a note:  

4 hours ago, Liam88 said:

$u->isLoggedIn()

This method do not exist, it's isLoggedin(), with `Logged` and `in` in lowercase. Just in case to avoid future hassle ?

Edited by flydev ??
wrong screenshot and note
  • Like 1
Link to comment
Share on other sites

  • 1 month later...
On 9/9/2022 at 12:00 PM, Liam88 said:

So I am using website.com/profile/user-id/ as the user profile page.

@Liam88 I've only skim-read your thread and I think @flydev probably has you covered, but one other thing to look out for is if you are using URL segments/pages etc that use the same terminology as used in the Login Register module.

I had an issue along these lines a while back where I was using the word 'profile' in a directory site and it conflicted with the login module's own profile setup. Ryan was going to patch it I believe so it may no longer be an issue. But thought I would mention it in case.

I've accidentally let my Login Pro subscription lapse so I can't see the details of our conversation at the moment but I know it had us puzzled for a while until Ryan spotted what was going on!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...