简体   繁体   中英

Redirect to the last page requested after login in cakephp

  1. HI I have search from where client can either click on keywords which is below in search bar or enter a keyword in search window, In case first when user click on keywords the search_results opens with keywords in URL but when user enter keyword then it just give result according to it.
  2. So when user not logged in then after some search_result we ask for login & on click login button we redirect for login popup from where user logged in that time we have to redirect on same page so for this I am using two concept one for predefined keywords link which come on URL just redirect last URL value in session & after logged in redirect to that URL.

3.I need help in keyword enter by user & then logged in so needs to redirect with same keyword page to user.

function search_result($adKeyword = null){

    if(!empty($adKeyword) && $adKeyword != ''){

        $this->data['City']['keyword'] = $adKeyword;
    }
    //////////////////Maintain fetch data////////////////////
    if(!empty($this->data)){    
        //pr($this->data);exit;
        if($this->data['City']['keyword'] == 'Name or Area of expertise'){
            $this->data['City']['keyword'] = '';
            $this->set("title_for_layout","Search Result");
        }

        if ( empty($this->data['City']['city_name'])) {

            $this->data['City']['city_name'] = $this->Session->read("Location");
        }

        if($this->data['City']['keyword'] != '')
            $this->set("title_for_layout",$this->data['City']['keyword']." | Search Result");
                       //$request_params = Router::getParams();
                          //$this->Session->write('auth_redirect','/'.$request_params['url']['url']);
                    $this->Session->write('login_referrer',$this->params['url']['url']);
                    $this->Session->write('login_referrers',$this->data['City']['keyword']);

above two session variable I am using for redirect after authentication on login

    if($this->Auth->user('role_id')== Configure::read('App.Role.Mentee')) {
                        if ($this->Session->check('login_referrer')) {
                                   $loginReferrer = $this->Session->read('login_referrer');
                                   $this->Session->delete('login_referrer');
                                   //prd($loginReferrer);  
                                   $this->redirect(SITE_URL."$loginReferrer");
                                   }
                                  else if($this->Session->check('login_referrers'))
                                   {

                                   $loginReferrers = $this->Session->read('login_referrers');
                                   $this->Session->delete('login_referrers');
                                   //prd($loginReferrers);  
                                   $this->redirect(array('controller'=>'fronts','action'=>'search_result/','$adKeyword' => $loginReferrers));
                                   }
                                  else {
                                  $this->redirect(array('controller'=>'fronts','action'=>'index'));
                                  }

what happening its not going to else if statement Please help me

If they are browsing some pages and asked to login then they should redirect to that page after login

That's exactly what the Auth component does by default . As such to get the desired behavior, the login function should look similar to:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

Note the use of $this->Auth->redirect() which returns the url to redirect the user to, this example is taken directly from the documentation .

To change the default Auth redirect url modify the loginRedirect property of the auth component (from your beforeFilter, for example).

For manually login I have solved my issue,I have added this code to search_result action of my front_controller

 $request_params = Router::getParams();
$this->Session->write('auth_redirect','/'.$request_params['url']['url']);

And in my user _controller I have just used this session variable:

if($this->Auth->user('role_id')== Configure::read('App.Role.Mentee')) {
$this->redirect($this->Session->read('auth_redirect'));
else
                                  $this->redirect(array('controller'=>'fronts','action'=>'index'));

But it is not solving my problem completely, I want to delete session value after one time redirect.but for this it is not destroying the router session variable value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM