Jump to content
totoff

Weird trouble with $session->redirect

Recommended Posts

Hello all,

I was absent for a while from developing - seems I got rusty in the meantime ... Having weird trouble with trying to redirect a page to its first child. I have the following structure:

- /section/ (template A)

-- /child 1/ (template B)

-- /child 2/ (template B)

and so on.

Template A has the following conditional:

if($page->numChildren) $session->redirect($page->child()->url);

However, whenever /section/ is clicked, it redirects to /child 2/ but not to /child 1/. This happens on every section with template A and children.

To make this even more weird, the redirect has worked perfectly fine for weeks and the strange behavior occurred today after editing content in the admin.

Template A sort settings == none

/section/ page sort settings == nothing selected

I've tried selector "sort=sort" but it changes nothing (/child 1/ is the oldest page). I've also tried to set child sort order on /section/ to manual-drag-and-drop. Again without success.

I'm quite helpless with this and would appreciate any help. Thanks.

Share this post


Link to post
Share on other sites

Does $page->children also show the children in the wrong order? Does explicit sorting work there?

Share this post


Link to post
Share on other sites

Hi LostKobrakai,

thanks for your post.

$page->children without selector echoes the correct order.

$content2 = $pages->get(1015)->children("sort=-sort");

returns /child 2/ as the last item in the list (that is, the oldest page). However, /child 1/ is definitely older as per its created timestamp ...

EDIT: corrected confusion of younger/older

Share this post


Link to post
Share on other sites

I don't know exactly, but does sort=sort not sort by the order of pages are in the pagetree? sort=created would sort by creation date. Also there shouldn't be a problem if children() returns the right order as child() internally calls children() and just returns the first page if there are children.

  • Like 1

Share this post


Link to post
Share on other sites

I don't know exactly, but does sort=sort not sort by the order of pages are in the pagetree?

Yes, you are right, it does. I was wrong.

Also there shouldn't be a problem if children() returns the right order as child() internally calls children()

I tried

$content2 = $pages->get(1015)->children->first;

and it returns /child 1/ (id 1023). However, the problem remains

if($page->numChildren) $session->redirect($page->child()->url);

still redirects to /child 2/ (id 1024) ...

Share this post


Link to post
Share on other sites
if($page->numChildren) $session->redirect($pages->get(1015)->children->first);

why not this way?

Or:

if($page->numChildren) $session->redirect($page->children->first);

Share this post


Link to post
Share on other sites

Hi Nico,

if($page->numChildren) $session->redirect($page->children->first->url);

works. But I would still like to understand why

$content2 = $pages->get(1015)->children("sort=-sort");

renders child 2 as child 1 while

$content2 = $pages->get(1015)->children("sort=sort");

renders the correct order. I mean, there is something wrong obviously ...

$pages->get(1015)->children->first

is not applicable as 1015 is just one out of many sections sharing the same problem.

Share this post


Link to post
Share on other sites

Do you mean sort=-sort is not just reverting the order, but mixing it up?

Yes, exactly.

$content2 = $pages->get(1015)->children("sort=sort");

renders 1023|1024|1031 which is correct

$content2 = $pages->get(1015)->children("sort=-sort");

renders 1031|1023|1024

See what I mean?

Share this post


Link to post
Share on other sites

Is it possible that 1023 and 1024 were created at the same moment? Because in that case, sort=sort acts like sort=created|id and sort=-sort would be sort=-created|id, this your real order would be something like (1023, 1024), 1031 and 1031, (1023, 1024).

(This is just a pseudocode, afaik there isn't anything like sort=created|id)

Share this post


Link to post
Share on other sites

Hi Adam,

Is it possible that 1023 and 1024 were created at the same moment?

Not according to the timestamp. 1024 was created three minutes later than 1023.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By stanoliver
      Hi! 
      The following code snippet is part of my markup simple navigation and the url_redirect (url field in the backend) just works fine when I put an special custom url into the url_redirect field.
      <?php $nav = $modules->get("MarkupSimpleNavigation"); // topnav $current = $page->rootParent(); // current root page // subnav echo $nav->render(array( 'max_levels' => 2, 'item_tpl' => '<h4><a class="g8-bar-item g8-hover-black" href="{url_redirect|url}">{title}</a></h4><hr class="sidenav">', 'collapsed' => true, ), $page, $current); ?>  In my seperated breadcrumb navigation I use the following code snippet
      <?php foreach($page->parents()->append($page) as $parent) { echo "<li><a href='{$parent->url_redirect|url}'>{$parent->title}</a></li>"; } ?> Now to the problem: In my first code snippet above the
      url_redirect|url 
      works just fine but when I try something similiar in the second code snippet
      $parent->url_redirect|url
      I produce an server error How do I have to change the second code snippet so that it works in the correct way as the first code snippet does?
    • By Orkun
      Hi Guys
      How can I make a redirect inside the .htaccess to my custom maintenance.html file when any URL of my Website is accessed except the processwire admin (www.example.com/processwire/).
      Because I want that my User's still can access the website when they are loggedin in Processwire.
      When the current url starts with /processwire or if there is a processwire-login-cookie (Is there a cookie when user is logged in Processwire?) available the redirect should not work. Otherwise it should work.
      How can I achieve this?
    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I'm trying to automatically redirect a logged-in user to a custom profile page using $session->redirect() and need to add $user->name to the redirect path.
      All my attempts appear to have failed:
      $session->redirect('/user-profile/')->name; $session->redirect('/user-profile/')->$user->name; $session->redirect('/user-profile/' . get($user->name . '/')); $session->redirect('/user-profile/' & get($user->name)); Can anyone point out where I am going wrong?
    • By Marco Angeli
      Hi there,
      I added a ssl certificate to my site and I'd like to redirect every single http url to its new https version
      So I added this code in the .htacces file, after the RewriteEngine On :
      Redirect 301 /about https://www.mysite.it/about
      Unfortunately this is now working: I get the "too many redirects" error.
      The following code works, but it's a bulk redirection to the home page, something I don't want for SEO reasons (https://moz.com/blog/save-your-website-with-redirects😞
      RewriteCond %{HTTP_HOST} mysite\.it [NC]
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.mysite.it/$1 [R,L]
      Any suggestions?
×
×
  • Create New...