表单动作链接到错误的控制器

时间:2011-09-21 19:50:17

标签: cakephp

我有一个控制器referral_medium_controller.php,其中包含以下内容

<?php
class ReferralMediumsController extends AppController
{
    var $name = 'ReferralMediums';

    function admin_index() 
    {
        //Checking that only super administrators can actually add new interest options
        if ($this->Auth->user('user_type_id') != ConstUserTypes::Admin) {
            $this->cakeError('error404');
        }

        $this->pageTitle = __l('Referral Mediums');
        $this->ReferralMediums->recursive = 0;
        $this->set('ReferralMediums', $this->paginate());
    }
    function admin_view($id = null) 
    {
        //Checking that only super administrators can actually add new interest options
        if ($this->Auth->user('user_type_id') != ConstUserTypes::Admin) {
            $this->cakeError('error404');
        }
        $this->pageTitle = __l('Referral Mediums?');
        if (is_null($id)) {
            $this->cakeError('error404');
        }
        $referralMedium = $this->ReferralMediums->find('first', array(
            'conditions' => array(
                'ReferralMedium.id = ' => $id
            ) ,
            'fields' => array(
                'ReferralMedium.id',
                'ReferralMedium.created',
                'ReferralMedium.modified',
                'ReferralMedium.referral_medium',
                'ReferralMedium.is_active',
            ) ,
            'recursive' => -1,
        ));
        if (empty($referralMedium)) {
            $this->cakeError('error404');
        }
        $this->pageTitle.= ' - ' . $referralMedium['ReferralMedium']['id'];
        $this->set('ReferralMediums', $referralMedium);
    }
    function admin_add() 
    {
        //Checking that only super administrators can actually add new interest options
        if ($this->Auth->user('user_type_id') != ConstUserTypes::Admin) {
            $this->cakeError('error404');
        }
        $this->pageTitle = __l('Add Referral Medium');
        if (!empty($this->data)) {
            $this->ReferralMedium->create();
            if ($this->ReferralMedium->save($this->data)) {
                $this->Session->setFlash(__l('Referral Medium has been added') , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(__l('Referral Medium could not be added. Please, try again.') , 'default', null, 'error');
            }
        }
    }
    function admin_edit($id = null) 
    {
        //Checking that only super administrators can actually add new interest options
        if ($this->Auth->user('user_type_id') != ConstUserTypes::Admin) {
            $this->cakeError('error404');
        }
        $this->pageTitle = __l('Edit Referral Medium');
        if (is_null($id)) {
            $this->cakeError('error404');
        }
        if (!empty($this->data)) {
            if ($this->ReferralMedium->save($this->data)) {
                $this->Session->setFlash(__l('Referral Medium has been updated') , 'default', null, 'success');
                $this->redirect(array(
                    'action' => 'index'
                ));
            } else {
                $this->Session->setFlash(__l('Referral Medium could not be updated. Please, try again.') , 'default', null, 'error');
            }
        } else {
            $this->data = $this->ReferralMedium->read(null, $id);
            if (empty($this->data)) {
                $this->cakeError('error404');
            }
        }
        $this->pageTitle.= ' - ' . $this->data['ReferralMedium']['id'];
    }
    function admin_delete($id = null) 
    {
        //Checking that only super administrators can actually add new interest options
        if ($this->Auth->user('user_type_id') != ConstUserTypes::Admin) {
            $this->cakeError('error404');
        }
        if (is_null($id)) {
            $this->cakeError('error404');
        }
        if ($this->ReferralMedium->del($id)) {
            $this->Session->setFlash(__l('Referral Medium deleted') , 'default', null, 'success');
            $this->redirect(array(
                'action' => 'index'
            ));
        } else {
            $this->cakeError('error404');
        }
    }
}
?>

带有以下内容的模型referral_model.php

<?php
class ReferralMedium extends AppModel
{
    var $name = 'ReferralMedium';

    var $useTable="referral_mediums";

    //$validate set in __construct for multi-language support
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasOne = array(
        'UserProfile' => array(
            'className' => 'UserProfile',
            'foreignKey' => 'referral_medium_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    var $hasMany = array(
        'UserProfile' => array(
            'className' => 'UserProfile',
            'foreignKey' => 'referral_medium_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    function __construct($id = false, $table = null, $ds = null) 
    {
        parent::__construct($id, $table, $ds);
        $this->validate = array(
            'referral_medium' => array(
                'rule' => 'notempty',
                'allowEmpty' => false,
                'message' => __l('Required')
            ) ,
        );
    }
}
?>

使用以下代码添加视图 referral_mediums / admin_add.ctp

<?php /* SVN: $Id: $ */ ?>
<div class="referralMedium form">
<?php echo $form->create('ReferralMedium', array('class' => 'normal'));?>
    <fieldset>
        <h2><?php echo __l('Add Referral Medium');?></h2>
    <?php
        echo $form->input('referral_medium');
        echo $form->input('is_active');
    ?>
    </fieldset>

   <div class="submit-block clearfix">
    <?php echo $form->submit(__l('Add'));?>
    </div>
    <?php echo $form->end();?>
</div>

现在一切正常,除非我点击添加表单中的添加按钮,我被重定向到admin / referral_media / add而不是admin / referral_medium / add,我找不到问题。我很高兴有人可以帮助我。

2 个答案:

答案 0 :(得分:2)

媒体的复数是媒体,而不是媒介。控制器名称是复数,因此它是referral_media。因此,您需要更改控制器名称(和文件名)。

答案 1 :(得分:0)

或者更改Config / bootstrap.php,这样它就不会再使控制器名称复数。