Cakephp redirects the wrong path

I have a login form (login.ctp) in my members view. But when I specify the url as / login, instead of showing the Members login, its users login page is displayed.

/Views/login.ctp file

<div class="members form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Member'); ?>
<fieldset>
    <legend><?php echo __('Please enter your username and password'); ?></legend>
    <?php echo $this->Form->input('username');
    echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div> 

      

Application controller file

class AppController extends Controller {
//...

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
        'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
    )
);

public function beforeFilter() {
    $this->Auth->allow('index', 'view');
}
//...
}

      

My route.php file

 Router::connect('/', array('controller' => 'members', 'action' => 'index'));
Router::connect('/login', array('controller' => 'members', 'action' => 'login'));

      

I cleared all cookies. Where is the mistake?

One more thing, I even tried to remove the custom controllers, but if I remove the custom controllers I get the following error ...

Error: UsersController not found.

This is my login function from MembersController.php

         public function login() {
    if ($this->request->is('post')) {

        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

      

// code in the member model.

class Member extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
public $validate = array(
    'firstname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'lastname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'email' => array(
        'email' => array(
            'rule' => array('email'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'password' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'age' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'address' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'phone' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'created_on' => array(
        'datetime' => array(
            'rule' => array('datetime'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);

function beforeSave() {
  if(isset($this->data[$this->alias]['password']))
    $this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], null, true);
  return true;
}
}

      

+3


source to share


2 answers


EDIT

Tested code and process

AppController Code

<?php
class AppController extends Controller
{
    public $components = array
    (
        'Session',
        'Auth' => array
        (
            'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
            'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
        )
    );

    public function beforeFilter()
    {
        $this->Auth->allow('index', 'view');
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        $this->Auth->userModel = 'Member';
    }
}

      



all your code is working fine, all you have to do is just install Auth->userModel

in Member

.

MemberController

<?php
class MembersController extends AppController
{

    var $name = 'Members';

    function beforeFilter()
    {
        parent::beforeFilter();
    }

}
?>
      

And in the Member controller define beforeFilter as above.

+1


source


Try changing your component array like this:

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
        'logoutRedirect' => array('controller' => 'members', 'action' => 'index'),
        'Form' => array(
            'userModel' => 'Member'
        ),
    )
);

      



Or try this in beforeFilter ():

// Pass settings in using 'all'
$this->Auth->authenticate = array(
    AuthComponent::ALL => array('userModel' => 'Member'),
    'Form',
    'Basic'
);

      

+1


source







All Articles