简体   繁体   中英

Cakephp 3 - The page isn’t redirecting properly

I am using Cakephp 3.x in which I have created one method inside my AppController ( checkBrandAssigned ) in which I have checked my logic and redirecting to the other page as per my needs. Below is how method looks like.

AppController.php

public function checkBrandAssigned(){

                $this->loadModel('Users');

                $user_data = $this->Users->find()
                    ->select(['user_id'])                    
                    ->where([                    
                        'user_type' => SALES_REPRESENTETIVE,
                        'user_status' => STATUS_INACTIVE,
                        'user_id NOT IN (SELECT ub_user_id FROM fp_user_brand)'
                    ])
                    ->order([ 'user_modified_date'=>'DESC'])
                    ->first();


                if (!empty($user_data)) {

                 return $user_data->user_id;   
                } else {
                    return '';
                }

    }

And below is my full snippet of AppController.php file.

<?php 


class AppController extends Controller {


    public function initialize() {
    parent::initialize();

    switch ($this->request->session()->read('Auth.User.user_type')):

        case COMPANY_ADMIN :

            $loginRedirectUrl = ['controller' => 'users', 'action' => 'dashboardCompanyAdmin'];

            break;

        default : $loginRedirectUrl = ['controller' => 'users', 'action' => 'dashboardSalesRep'];
            break;
        endswitch;       
   }



    public function checkBrandAssigned(){

                $this->loadModel('Users');

                $user_data = $this->Users->find()
                    ->select(['user_id'])                    
                    ->where([                    
                        'user_type' => SALES_REPRESENTETIVE,
                        'user_status' => STATUS_INACTIVE,
                        'user_id NOT IN (SELECT ub_user_id FROM fp_user_brand)'
                    ])
                    ->order([ 'user_modified_date'=>'DESC'])
                    ->first();


                if (!empty($user_data)) {

                 return $user_data->user_id;   
                } else {
                    return '';
                }

    }
}

As you can see above, I have used checkBrandAssigned method inside my beforeFilter and if my condition gets true, I am redirecting at, $this->redirect(['controller' => 'Users', 'action' => 'brandRetailerView','?'=>['repId'=>'6b12337d37870e4afb708a20f4f127af']]); .

Below is how my UsersController.php file.

UsersController.php

class UsersController extends AppController {

    public function initialize() {
        parent::initialize();
    }

    public function beforeFilter(Event $event) {    
        parent::beforeFilter($event);                
    }


    public function brandRetailerView() {
        die('inside my function');

    }

}

As you can see I have put die('inside my function'); I am able to see this text, howerver, If I comment it, I am getting error saying...

The page isn't redirecting properly

I have also created blank brand_retailer_view.ctp file in my respective location. Below is screenshot of my error.

在此处输入图片说明

I have also used Auth Component . I want brandRetailerView and checkBrandAssigned methods to be used after login hence I have not added inside my $this->Auth->allow in AppController.php and UsersController.php . However, I tried to add checkBrandAssigned in $this->Auth->allow , but still its not working.

I have tried to use return parent::beforeFilter($event); in my UsersController.php but its not working as well.

I have not added anything extra inside my .htaccess file, hence I am not adding here.

Can someone help me why I am having this issue. Even if I use die its working but as soon as I comment it, I am getting above error.

the issue is not in the code, it's in the logic

the issue is because irrespective of the condition returned by checkBrandAssigned(), the redirect will be to users controller and before filtering the user controller the condition will be checked again and will again be redirected to the user controller.

it will become an infinite loop of redirection, in case of die() it is working because die() break the loop before the redirect happens.

to solve this remove the redirect check from beforeFillter and put somewhere else...

add this to enable cookies in AppController.

use Cake\Controller\Component\CookieComponent;

and update the condition as follows.

if (!isset($this->Cookie->read('checkedInLast30Secs'))) {
        $this->Cookie->write('checkedInLast30Secs',
            [
                'value' => true,
                'expires' => '+30 secs'
            ]
        );
        if (!empty($checkSalesRepId)) {
            $this->redirect(['controller' => 'Users', 'action' => 'brandRetailerView', '?' => ['repId' => '6b12337d37870e4afb708a20f4f127af']]);

            //     $this->dashboardUrl = ['controller' => 'users', 'action' => 'brandRetailerView'];
        } else {
            $this->dashboardUrl = ['controller' => 'users', 'action' => 'dashboardCompanyAdmin'];
        }
    }

Eventually I have solved with below code.

case COMPANY_ADMIN :

  if (!empty($checkSalesRepId)) {

    if($this->request->params['action'] != 'brandRetailerView'){
        $this->redirect(['controller' => 'Users', 'action' => 'brandRetailerView','?'=>['repId'=>$checkSalesRepId]]);
    }                        
    else {
        $this->dashboardUrl = ['controller' => 'users', 'action' => 'dashboardSalesManager'];
    }

}

Here I am simply checking if the current action is not brandRetailerView . If so, I am redirecting to brandRetailerView page, else dashboardSalesManager page.

Hope this helps.

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