request->getData isnt working on cakephp3.6


#1

I upgraded my cakephp to ver 3.6 . I cant rewrite this code that worked in cakephp3.2 to make it work in 3.6 to parse get data for a search

Whats going on here as the message doesnt make sense? I tried getParam and I get same error?

 Accessing routing parameters through `getData` will removed in 4.0.0. Use `getParam()`    

if ($this->request->is('post')) {

     if ($this->request->getData('search')!=null ) { 
               $filter_url['controller'] = $this->request->getParam('controller');
               $filter_url['action'] = $this->request->getParam('action');
                $filter_url['page'] = 1;
        foreach($this->request->getData as $name => $value){ //error here

            if($value){
                $filter_url[$name] = urlencode($value);

            }
                }
            return $this->redirect($filter_url);

          }//if isset     


    } // if post

#2

I re-wrote your code. The code I wrote should help you understand what the problem was.

if ($this->request->is(‘post’)) {

	$search = $this->request->getData('search');
	
	if (!empty($search)) {
		$filter_url['page'] = 1;
		$filter_url['action'] = $this->request->getParam('action');
		$filter_url['controller'] = $this->request->getParam('controller');
		$requestData = $this->request->getData(); //function not a property defined in an object
		/*
			You were trying to get the data from a property in an object instead of calling the 
			CakePhp pre-defined function that does that.
			
			You could have:				
			foreach($this->request->getData() as $key => $value) {
					
			}
			
			Or define a variable to hold the data like I have done and then run the foreach
		*/
		foreach($requestData as $name => $value) {
			if($value) {
				$filter_url[$name] = urlencode($value);
			}
		}
		
		unset($requestData);
		
		return $this->redirect($filter_url); 
	}
}

I hope it helps.