Abdus Sattar Bhuiyan
Abdus Sattar Bhuiyan

Reputation: 3074

How to get url params in cakephp?

I am using cakephp 2.6.7: I have a password reset url like: http://www.jegeachi.com/resellers/resetpw/9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/

Here resellers is the controller and resetpw is the action and 9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12 is the argument I want to pass into resetpw function.

Now in 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 --> 

Inside resellers action:

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'));

    }    
}

Here I want to catch the url data 9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12 But none of the following printing my data:

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

How can I get this url value.

Upvotes: 1

Views: 3174

Answers (3)

skywalker
skywalker

Reputation: 826

As I said in one of my comments on your previous questions, you should first read the docs. What you are doing in this action is wrong on couple of levels, in cakephp sense. Normally you would get to this action by url I suppose

.../resellers/resetpw/9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/

and by that you would already have that action argument in your controller:

public function resetpw($a = null)

Then you would check if $a is not null since you can't reset password without some kind of token. In your action $a already has this for value 9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12. No need to use something like this:

$a = func_get_args();

Second thing, you should name arguments to be meaningful, $a doesn't mean nothing, but $token or something similar would make much difference.

Upvotes: 2

Abdus Sattar Bhuiyan
Abdus Sattar Bhuiyan

Reputation: 3074

I solved this by removing 'url' in Form.

I changed

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

To

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

                    )
            );
            ?>

Now it works. When you set url then parameter in url is removed.

Upvotes: 0

Anand G
Anand G

Reputation: 3210

There are multiple ways of doing it. In your function below (not writing entire one) you are trying to get the url argument in $a but you are passing parameter instead. Hence $a has no value

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;

In your case you need catch params like below

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

Or to get argument your URL should be like below

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

so that you can collect it in $a. Hope this helps to you!

Upvotes: 0

Related Questions