如何在cakephp中获取url params?

时间:2015-12-11 09:29:32

标签: cakephp

我正在使用cakephp 2.6.7: 我有一个密码重置网址: http://www.jegeachi.com/resellers/resetpw/9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/

这里的经销商是控制器,resetpw是动作,9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12是我要传递给resetpw函数的参数。

现在在resetpw.ctp:

<style type="text/css">
    .alert {
        padding: 6px;
        margin-bottom: 5px;
        border: 1px solid transparent;
        border-radius: 4px;
        text-align: center;
    }
    .alert.alert-error {
        background: #A9B0B5;
        color: #EF5858;
        font-weight: normal;
    }

</style>
<div class="page-content-wrapper">
    <div class="page-content">
        <!-- BEGIN PAGE CONTENT-->
        <div class="content">

            <!-- BEGIN LOGIN FORM -->

            <?php
            echo $this->Form->create('Reseller', array(
                'inputDefaults' => array(
                    'label' => false,
                    'div' => false
                ),
                'class' => 'login-form',
                'url' => array('controller' => 'resellers', 'action' => 'resetpw')
                    )
            );
            ?>




            <h3 class="form-title">Type your new password</h3>
            <div class="alert alert-danger display-hide">
                <button class="close" data-close="alert"></button>
                <span>
                    Enter your new password first. </span>
            </div>

            <div class="form-group">
                <label class="control-label visible-ie8 visible-ie9">New Password</label>
                <div class="input-icon">
                    <i class="fa fa-lock"></i>

                    <?php
                    echo $this->Form->input(
                            'password', array(
                        'class' => 'form-control placeholder-no-fix',
                        'type' => 'password',
                        'autocomplete' => 'off',
                        'placeholder' => 'New Password'
                            )
                    );
                    ?>
                </div>
            </div>

            <div class="form-actions">
                <?php
                echo $this->Form->button(
                        'Reset Password <i class="m-icon-swapright m-icon-white"></i>', array(
                    'class' => 'btn blue pull-right',
                    'type' => 'submit',
                    'escape' => false
                        )
                );
                ?> 

            </div>

            <?php echo $this->Form->end(); ?>
            <!-- END LOGIN FORM -->


        </div>
        <!-- END PAGE CONTENT -->
    </div>
</div>
<!-- END CONTENT --> 

内部转销商行动:

function resetpw($a){
    $this->layout = "public-login";
    $this->loadModel('Reseller');
    if ($this->request->is('post')) {
        echo 'a : '.$a;
        pr($this->params['url']); exit;
        $a = func_get_args();
    $keyPair = $a[0];
    $key = explode('BXX', $keyPair);
    $pair = explode('XXB',$key[1]);
    $key = $key[0];
    $pair = $pair[1];
    $password = $this->request->data['Reseller']['password'];
    unset($this->request->data['Reseller']['password']);
    $uArr = $this->Reseller->findById($pair);
    if($uArr['Reseller']['resetkey'] == $key) {
        $this->Reseller->read(null, $pair);
        $this->Reseller->set('password', $password);
        if($this->Reseller->save()) { 
            $msg = ' <div class="alert alert-success">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            <strong>Your password has been reset</strong> 
                        </div>'; 
        } else {
              $msg = ' <div class="alert alert-error">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            Something has gone wrong. Please try later or <strong>sign up again</strong> 
                        </div>'; 
            }
        } else {
            $msg = ' <div class="alert alert-error">
                <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>Invalid link.Please check your reset link</strong> Invalid link.Please check your reset link
                        </div>'; 
        } 

        $this->Session->setFlash($msg);

        $this->redirect(array('controller'=>'resellers', 'action' => 'login'));

    }    
}

我想抓住网址数据 9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12 但以下都没有打印我的数据:

    echo 'a : '.$a;
    pr($this->params['url']); exit; 

如何获取此网址值。

3 个答案:

答案 0 :(得分:2)

正如我在你之前的一些问题的评论中所说,你应该先阅读文档。在cakephp意义上,你在这个动作中所做的事情在几个层次上都是错误的。通常你会通过url来实现这个动作我想

.../resellers/resetpw/9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/

并且你的控制器中已经有了这个动作参数:

public function resetpw($a = null)

然后你会检查$a是否为空,因为你不能在没有某种令牌的情况下重置密码。在您的操作中$a已经为值9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12设置了此值。 不需要使用这样的东西:

$a = func_get_args();

第二件事,你应该将参数命名为有意义,$a并不意味着什么,但$token或类似的东西会产生很大的不同。

答案 1 :(得分:0)

有多种方法可以做到这一点。在下面的函数中(不是写整个函数),您试图在$a中获取url参数,但是您正在传递参数。因此$a没有价值

function resetpw($a){
    $this->layout = "public-login";
    $this->loadModel('Reseller');
    if ($this->request->is('post')) {
        echo 'a : '.$a; //$a will have no values here
        pr($this->params['url']); exit;

在你的情况下,你需要像下面那样抓住参数

$a = $this->params['passed'][0];

要获得参数,您的网址应如下所示

http://www.jegeachi.com/resellers/resetpw?9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/

以便您可以在$a中收集它。希望这对你有所帮助!

答案 2 :(得分:0)

我通过删除表单中的“url”解决了这个问题。

我改变了

  <?php
            echo $this->Form->create('Reseller', array(
                'inputDefaults' => array(
                    'label' => false,
                    'div' => false
                ),
                'class' => 'login-form',
                'url' => array('controller' => 'resellers', 'action' => 'resetpw')
                    )
            );
            ?>

  <?php
            echo $this->Form->create('Reseller', array(
                'inputDefaults' => array(
                    'label' => false,
                    'div' => false
                ),
                'class' => 'login-form',

                    )
            );
            ?>

现在它有效。设置url后,将删除url中的参数。