21282 Posts in 5730 Topics by 2601 members
General Questions
SilverStripe Forums » General Questions » How to render page as normal if a condition isn't met
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 249 Views |
-
How to render page as normal if a condition isn't met

5 July 2012 at 9:51pm Last edited: 9 July 2012 8:18pm
Hi guys,
EDIT:
Sorted the first question!
First question:
I was creating an imaginary customer login section on my site, and wanted to show them a login form if they didn't have cookies set for username and password, or show them their personal page if they'd logged in & had the credentials stored correctly. Just as a learning task.
The problem I had was, while I knew how to render a page different from the one I was currently in the controller for (in this case, if they were logged in), but I didn't know how to render the original page manually (my 'if' statement needed an 'else'). Here was the solution:
if (($email = $cookie->get('ClientEmail')) && ($pass = $cookie->get('ClientPassword')))
{
$where = "Client.ClientEmail = '" . $email . "'";
$where .= " AND Client.ClientPassword = '" . $pass . "'";
if($client = DataObject::get_one("Client", $where, "", "", ""))
{
return $this->customise(array('Client' => $client))->renderWith(array('ClientPage', 'Page'));
}
else
{
$cookie = new Cookie;
$cookie->forceExpiry('ClientEmail');
$cookie->forceExpiry('ClientPassword');
$this->setMessage('bad', 'Invalid session');
Controller::redirectBack();
}
}
else
{
// This is the code I was originally asking for, ClientHolder is the template & controller I am currently working in
$page = Director::get_current_page();
return $this->renderWith(array('ClientHolder', 'Page'));
}Bonus question:
In this 'if' statement
if (($email = $cookie->get('ClientEmail')) && ($pass = $cookie->get('ClientPassword')))
I had to evaluate both conditions separately, whereas normally I would remove the extra brackets like this
if ($email = $cookie->get('ClientEmail') && $pass = $cookie->get('ClientPassword'))
However, with the brackets removed, $email always returned '1'. Any idea why this would be? Thanks
-
Re: How to render page as normal if a condition isn't met

6 July 2012 at 5:41am Last edited: 6 July 2012 5:41am
Hi!
It would have been nice if you had left the original question in place and just added the answer - that way other people can find it.Regarding your bonus question: I think it has to do with operator precedence. In addition to the well known "multiplication before addition", there are rules as to which operators are evaluated first. The assignment operator (=) is evaluated after &&. It is one of the last ones, see http://at2.php.net/manual/en/language.operators.precedence.php
Without brackets, php probably interprets your code as:
if ($email = ($cookie->get('ClientEmail') && ($pass = $cookie->get('ClientPassword'))))If you really want to loose the brackets, you can use "and" instead of "&&", but I would strongly recommend to keep them. If someone else (or you yourself) later comes across the code, he will have no clue why the "and" is there.
-
Re: How to render page as normal if a condition isn't met

9 July 2012 at 8:20pm Last edited: 9 July 2012 8:22pm
Hi Jak,
Thanks for clearing that up, I always had a feeling there was a difference between '&&' and 'and', but was never sure what! I usually use 'AND' (capitalised for readability) but it seems this would've been below '&&' in the order of precedence anyway.
I've also edited my original post and readded the first question
Thanks
-
Re: How to render page as normal if a condition isn't met

11 July 2012 at 8:29pm
Your welcome, and thanks for putting the question and answer back!
I actually think it's a really bad idea that PHP has these subtle differences. It's just not something you would expect if you don't look it up.
| 249 Views | ||
|
Page:
1
|
Go to Top |


