Hello,
I’m using CakePHP 3.5 and here is my problem :
Using the Auth component, I deny access to certain methods to unauthenticated users
e.g : the URL /notification : if an unauthenticated users try to access it , the framework give me an error like this :
http://localhost/mywebsite/login?redirect=%2Fnotifications
My question is : how i can get acess to this redirect variable and redirect , after login, to /notifications for example
i tried to use return $this->redirect($this->request->getParam('redirect'));
without success
Thanks
in login()
First you need to get redirect value
like
$redirectUrl = $this->request->getQuery(‘redirect’);
.
.
.rest of login code here
.
.
After login success
return $this->redirect($redirectUrl);
Hello and thanks for the reply,
I did what you suggested however, it only works half
I used the $redirectUrl and i get the good value but return $this->redirect($redirectUrl); doesn’t redirect to the page but to /login
login work but not the redirection
are you maintain action value in route?
can you send me your login(){} code for check?
What you mean by “are you maintain action value in route?” ?
My login look like this :
public function login()
{
$this->viewBuilder()->layout('error');
$this->set('title' ,'Connexion required');
$redirectUrl = $this->request->getQuery('redirect');
if ($this->request->is('post'))
{
$user = $this->Auth->identify();
if ($user)
{
$this->Auth->setUser($user);
return $this->redirect($redirectUrl);
}
else {
$this->Flash->error(__('Wrong username or password'));
return $this->redirect($this->Auth->logout());
}
}
}
$url =“http://localhost/projectName ” ;
$redirectUrl = $this->request->getQuery(‘redirect’);
return $this->redirect($url. $redirectUrl);
This is working for me.
i’m sorry but not for me i’m redirecting to $url only, this is like , after login, that $redirectUrl doesn’t exist anymore.
after a few tests, that’s it, the $redirectUrl doesn’t exist anymore after if ($this->request->is('post')) {
christ57:
if ($user) {
pj($redirectUrl);
pj($user);
exit();
check you are getting user and redirectUrl data or not, after login
Getting user data but redirectUrl = null
Ok
Don’t why you do not get data form url.
On my project it is working perfectly.
Can you show me how you did that please and which version of CakePHP you used ?
in cakephp 3.6 and 3.7 it is working.
$url =“http://localhost/projectName” ;
$redirectUrl = $this->request->getQuery(‘redirect’);
if ($this->request->is(‘post’))
{
$user = $this->Auth->identify();
if ($user) {
if($redirectUrl){
return $this->redirect($redirectUrl);
}else{
return $this->redirect($url);
} }else{
$this->Flash->error(__(‘Wrong username or password’));
return $this->redirect($url);
}
ok thanks, i’m using cakephp 3.5 so i will put $this->request->getQuery(‘redirect’); in a hidden field
by the way thanks a lot for your help !
no need to put this in hidden field.
http://localhost/mywebsite/login?redirect=%2Fnotifications
in controller it will direct get this value from url