Can I redirect from a view in cakephp
I have a button on my home page which leads to a signup/login page. The signup/login page is just a page in views/pages/signup.ctp and as such doesn't have an action associated with it.
I 开发者_JAVA百科want it so if a user is already logged in when they're on the homepage and they click my signup/login button, instead of taking them to the signup/login page it redirects them to another action. But since the signip/login page doesn't have an action I don't know where to do the check and redirect. Is there a way of checking if a persons logged in in the signup.ctp view? and redirect them from there?
Thats probably miles away from reality but I'm very confused. Any help appreciated. Cheers :)
Create an action for signup... and you can do the verification there. I don't think it's a good practice to put redirects in views. You can place the action in the Homepage controller for example...
Even if it was pretty to put the redirect in the view, you would need to set a variable (in an action) to check if user is logged in or not. :)
you can put a different link for the button in your home page..
i.e. depending on the user's status (logged in or not), you will choose which link to associate with the button..
I don't know if you're using the Auth
component..
if so, in your home page's template you can do something like:
<?php
if ($session.read('Auth.User')) {
// the user is logged in
// put the link to the other action
}
else {
// the user isn't logged in
// put the link to the signup/login page
}
?>
this way, you will not need to redirect the user...
hope this helps..
good luck with your development...
Do this in your ExampleController for the specific view like "example.ctp"
public function example(){
if ($session.read('Auth.User')) {
this->redirect(['action' => 'exampleview']);
}
}
Hope that it will solve your purpose.
You can use
header ("Location: ".$this->Html->url('/'));
You can also use
header ("Location: ".$this->Html->url(array('controller'=>'Home','action'=>'index')));
for using controller and action in cakephp
精彩评论