Jump to content

Best way of redirecting to a page


fedeb
 Share

Recommended Posts

Hi!

I have a search bar that should redirect to a page if the query matches a page title. Based on suggestions I read on the forum I implemented this as follows:

$protein_page = $pages->get("template=protein, title='$search_input'");

if(!($protein_page instanceof NullPage)) {
	$session->redirect($protein_page->url);
}
else{
	echo "No results for that query!";
}

Do I really need to retrieve a $page object only to get the url? I actually know the url since it is simply mysite/proteins/$search_input.

Is Process Wire smart enough to render the page based on the info retrieved in $protein_page or should I simply use:

$session->redirect("mysite/proteins/".$search_input);

In this case I don't know how to avoid the redirect if the url doesn't exist.

Thanks in advance!

Link to comment
Share on other sites

Hi, of course you can use the redirect-function just with a url based on the input. This is basically what you provide PW with $protein_page->url.

But I'm not quite sure if this makes a big difference in your case since you're using $pages->get before. So just try out what works better / more performant for you. 

 

Link to comment
Share on other sites

Hi, thanks for the reply.

My problem is that I want to avoid to query the database twice. Am I doing that in the first snippet?

1 - I get all of the data from the page, this is all of its fields, just to use the url attribute

2- I do the $session->redirect(), thus I have to retrieve again all of the information of the specific page to populate the template

It seems like I am doing two queries.

On the other hand the second snippet would avoid this but I don't know how to avoid the redirect if the url() doesn't exists.

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...