fedeb Posted June 30, 2021 Posted June 30, 2021 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!
Daniel Zellfelder Posted June 30, 2021 Posted June 30, 2021 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.
fedeb Posted June 30, 2021 Author Posted June 30, 2021 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.
Daniel Zellfelder Posted June 30, 2021 Posted June 30, 2021 I see… so maybe $pages->getRaw() is the right thing for you? $protein_url = $pages->getRaw("template=protein, title='$search_input'", "url"); https://processwire.com/api/ref/pages/get-raw/ 1
fedeb Posted July 1, 2021 Author Posted July 1, 2021 Thanks, I will give it a look and do some tests to see if I gain any speed! If I find something useful I'll update the thread.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now