Shane
Shane

Reputation: 851

Where should I put this method in CakePHP-driven site?

I am writing an application using CakePHP and I am unsure as to where I should be putting my generateMapUrl function.

function generateMapUrl($id = null) {
    if ( !$id ) {
        $this->Session->setFlash('Invalid Property Id');
    } 
    $this->read(null, $id);
    $url = "http://maps.google.com/maps?oi=map&q=";
    $url .= $this->data['street_num'] . '+';
    $url .= $this->data['street'] . '+';
    $url .= $this->data['city'] . '+';
    $url .= $this->data['province'] . '+';
    $url .= $this->data['country'] . '+';
    $url .= $this->data['postal_code'];

    return $url;
}

I have the following structure:

Booking (Model & Controller)
Properties (Model & Controller)
Address (Model & Controller)

A Booking hasOne Property and a Property hasOne Address. I would like to be able to call generateMapUrl for any Address. I am unsure as to where to put the method though... Address controller? Address Model? (Note: I am calling this method from the Bookings controller)

Upvotes: 0

Views: 284

Answers (5)

neilcrookes
neilcrookes

Reputation: 4203

The generateMapUrl() method should be a method in your Address model as it is dealing with fetching and formatting Address data, but should return false and not contain the session calls:

function generateMapUrl($id = null) {
  $this->recursive = -1;
  if (!$this->read(null, $id)) {
    return false;
  } 
  $url = "http://maps.google.com/maps?oi=map&q=";
  $url .= $this->data['street_num'] . '+';
  $url .= $this->data['street'] . '+';
  $url .= $this->data['city'] . '+';
  $url .= $this->data['province'] . '+';
  $url .= $this->data['country'] . '+';
  $url .= $this->data['postal_code'];  
  return $url;
}

You can then call this from any controller and use the session calls there:

function x() {
  if (!$mapUrl = ClassRegistry::init('Address')->generateMapUrl($addressId)) {
    $this->Session->setFlash('Invalid Property Id');
  }
}

Upvotes: 0

Saem
Saem

Reputation: 3413

At the very least, there are three things happening:

1) Creation of the URL 2) Error handling 3) And though not explicit, the actual call.

The creation of the URL should be handled by a dedicated helper class. It is likely to best to have the check carried out on the controller, the actual checking code would reside elsewhere. The error handling should be on the controller, as it would have to do anything special required for the specific place the user is at. Lastly, the call itself, should of course take place on the controller.

Upvotes: -1

Andrew Clark
Andrew Clark

Reputation: 1403

In the model build a GoogleAddress Class. Then use the GoogleAddress in the controller.

Upvotes: 0

RibaldEddie
RibaldEddie

Reputation: 5136

I might consider creating an AddressMapUrlGenerator class and using it to create the URL. You could pass it an Address. That way, you will not be muddling the Model with worries about urls or maps, and you will not have future worries about having the same function spread out in more than one Controller.

Upvotes: 0

Hawk Kroeger
Hawk Kroeger

Reputation: 2334

In the Controller, it has session data. The Model should not be aware of any session states.

Upvotes: 3

Related Questions