title: CakePHP 3 : display data from other model and pass parameter in url from action
body: <p>I'm working on a project using CakePHP 3.x.</p>
  
  <p>I have <code>UserAddress</code>, <code>ServiceRequests</code>, <code>Service</code> models.</p>
  
  <p>There is a button on <code>service/view/$id</code> which when clicked will ask user to select address from <code>service-requests/serviceArea</code> which has a list of addresses added by user. <code>service-requests/serviceArea</code> view will contain a <code>select</code> button which when clicked will call <code>add</code> action in <code>ServiceRequests</code> controller with passing two parameters <code>serviceId</code> and <code>userAddressId</code></p>
  
  <p>This is the serviceArea function created by me.</p>
  
  <pre><code>public function serviceArea($id = null)
      {
          public $uses = array('UserAddress');
          $service = $id;
          $query = $userAddresses-&gt;find('all')
            -&gt;where(['UserAddresses.user_id =' =&gt; $this-&gt;Auth-&gt;user('id')]);
  
  
  
          $this-&gt;set(compact('userAddresses'));
          $this-&gt;set('_serialize', ['userAddresses']);
      }
  </code></pre>
  
  <p>How to display the address and also pass the <code>$service</code> parameter to the <code>serviceArea</code> view.</p>
  
  <p>I am new to CakePHP, so if you think question is incomplete any edit to it will be appreciated instead of down-voting.</p>
  
  <p>Thank You.</p>
  
  <hr>
  
  <blockquote>
    <p>Edit 2</p>
  </blockquote>
  
  <p>Thank for your answer @jazzcat</p>
  
  <p>After changing my code according to yours and visiting <code>http://domain.com/service-requests/service-area/$id</code>. It is showing error as
  <code>Record not found in table "service_requests"</code></p>
  
  <p>and pointing to the <code>ServiceRequestsController</code> on line no <strong>33</strong></p>
  
  <p>The <code>ServiceRequestController</code> as containing line no 33 is </p>
  
  <pre><code>&lt;?php
  namespace App\Controller;
  
  use App\Controller\AppController;
  
  /**
   * ServiceRequests Controller
   *
   * @property \App\Model\Table\ServiceRequestsTable $ServiceRequests
   */
  class ServiceRequestsController extends AppController
  {
  
      /**
      * isAuthorized method
      *
      */
      public function isAuthorized($user)
      {
        $action = $this-&gt;request-&gt;params['action'];
  
        // The add and index actions are always allowed.
        if(in_array($action, ['index', 'add', 'serviceRequests'])) {
          return true;
        }
        // All other actions require an id.
        if (empty($this-&gt;request-&gt;params['pass'][0])) {
          return false;
        }
  
        // Check that the service request belongs to the current user.
        $id = $this-&gt;request-&gt;params['pass'][0];
        $serviceRequest = $this-&gt;ServiceRequests-&gt;get($id);  // line : 33
        if($serviceRequest-&gt;user_id == $user['id']) {
          return true;
        }
        return parent::isAuthorized($user);
      }
  
          /* Other actions */
  }
  ?&gt;
  </code></pre>