未知方法"允许"

时间:2015-05-12 16:32:48

标签: cakephp cakephp-3.0

所以我有这个控制器和方法:

  

Contorller / UsersController.php

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Exception\NotFoundException;
use Cake\ORM\TableRegistry;


/**
 * Jobs Controller
 *
 * @property \App\Model\Table\JobsTable $Jobs
 */
class UsersController extends AppController
{
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Users->allow('register');
    }

    public function register()
    {
        //Get Categories
        $getCategories = TableRegistry::get('Categories');
        $categories = $getCategories->find('all');
        $this->set('categories',$categories);

        $this->set('title', 'Registration');


        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'register']);
            }
            $this->Flash->error(__('Unable to add the user.'));
        }
        $this->set('user', $user);
    }

}
  

型号/表/ UsersTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;

class UsersTable extends Table
{
    public function validationDefault(Validator $validator)
    {
        return $validator
            ->notEmpty('first_name', 'A username is required')
            ->notEmpty('last_name', 'A username is required')
            ->notEmpty('email', 'A username is required')
            ->notEmpty('username', 'A username is required')
            ->notEmpty('password', 'A username is required')
            ->add('role', 'inList', [
                'rule' => ['inList', ['Employer', 'Job Seeker']],
                'message' => 'Please enter a valid role'
            ]);
    }

}
?>
  

模板/用户/ register.ctp:

<div class="users form">
<?php echo $this->Form->create($user); ?>
    <fieldset>
        <legend><?= __('Create New User'); ?></legend>
        <?php 
            echo $this->Form->input('first_name');
            echo $this->Form->input('last_name');
            echo $this->Form->input('email',array('type'=>'email'));
            echo $this->Form->input('username');
            echo $this->Form->input('password',array('type'=>'password'));
            echo $this->Form->input('confirm_password',array('type'=>'password'));
            echo $this->Form->input('role',array(
                                    'type' => 'select',
                                    'options' => ['Employer'=>'Employer','Job Seeker'=>'Job Seeker'],
                                    'empty' => 'Select Role'
                                    )); 
        ?>
    </fieldset>
<?= $this->Form->button(__('Submit')); ?>
<?= $this->Form->end() ?>
</div>
  

实体/ user.php的:

<?php
namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

/**
 * User Entity.
 */
class User extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * @var array
     */
    protected $_accessible = array(
        'first_name' => true,
        'last_name' => true,
        'email' => true,
        'username' => true,
        'password' => true,
        'role' => true,
        'created' => true
    );
    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }
}

所以当我试图访问/ users / register视图时,它会给我下一个错误:

  

未知方法&#34;允许&#34;:$ this-&gt;用户 - &gt;允许(&#39;注册&#39;);

但是如果我删除了我的Users控制器中的下一个方法:

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->Users->allow('register');
}

它工作正常,但我想它也必须使用它。那么我做错了什么或错过了什么?

0 个答案:

没有答案