Otori Takeo
Otori Takeo

Reputation: 1

CakePHP URL Parameter

How can I get the parameter in the URL and save it to a varialble so I can save it to my database?

example: www.mydomain.com/item/products/3 <-

This is for my upload image, so I can specify what product ID will I use for that image.

function add() {
        if ($this->request->is('post')) {
            $this->Upload->create();

                if(empty($this->data['Upload']['image']['name'])) {
                    unset($this->request->data['Upload']['image']);
                }

                if(!empty($this->data['Upload']['image']['name'])) {
                    $filename = $this->request->data['Upload']['image']['name'];
                    $new_filename = STring::uuid().'-'.$filename;
                    $file_tmp_name = $this->request->data['Upload']['image']['tmp_name'];
                    $dir = WWW_ROOT.'img'.DS.'uploads';

                    move_uploaded_file($file_tmp_name,$dir.DS.$new_filename);

                    $this->request->data['Upload']['image'] = $new_filename;
                    if($this->Upload->save($this->request->data)) {
                        $this->Session->setFlash(__('Uploaded.'));
                        $this->redirect(array('action' => 'index'));
                    }
                }
        }
    }

How will I add it here. Thank you in advance :) Im using cakePHP

Upvotes: 0

Views: 108

Answers (2)

ForsakenSigma
ForsakenSigma

Reputation: 5

For example in a controller method like the following

public function product($id) {
  .....
}

You access it by the url (for example): www.mydomain.com/item/products/3 where Item is the controller, product is the method you are calling in that controller and 3 represent a parameter that is required to the function to work in this case $id. (assuming you don't have any routing configuration)

Is treated as a normal php variable, just do whatever you wanna do with it. Just make sure you pass the correct value

Upvotes: 0

rvegas
rvegas

Reputation: 78

Just add the product id as a hidden input in the Form. Then it will be included in the $this->data variable when you get the POST request.

Upvotes: 1

Related Questions