[TYPO3-mvc] A question about MVC when an extension doesn't have it's own table
Stephen Bungert
stephenbungert at yahoo.de
Tue Nov 22 14:45:22 CET 2011
Hello,
mybe some of you with more programming knowledge can help me here.
I'm making an extbase extension that impliments the flickr API, I want it
because I have tt_news articles and I want to use tt_news's tag field to
query flicker for images with these tags and show a galllery of matching
image sin my article. I also want to the plugin to take page keywords or a
record's keywords (if on some kind of single record page) and show images.
My problem is, ist that my other extbase extensions had tables...
This time I don't need any tables, I just query flicker with information
provided by TS to build the query.
As I don't have any repositories and any tables I just created a model
called "Tx_Flickrimages_Domain_Model_Flickr", and in it I put some variables
and functions that I need in order to display things how I want in my views.
Here's how it looks so far (just testing at the moment):
<?php
/**
*
*
* @package flickrimages
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public
License, version 3 or later
*
*/
class Tx_Flickrimages_Domain_Model_Flickr extends
Tx_Extbase_DomainObject_AbstractEntity {
/**
* Flickr's tag limit when searching by tag
*
* @var integer
*/
const maxtags = 20;
/**
* flickr params description
*
* @var array
*/
protected $params;
/**
* flickr url description
*
* @var string
*/
protected $url;
/**
* Returns the maxtags
*
* @return integer $maxtags
*/
public function getMaxtags() {
return self::maxtags;
}
/**
* Returns the params
*
* @return array $params
*/
public function getParams() {
return $this->params;
}
/**
* Sets the params
*
* @param array $params
* @return void
*/
public function setParams(array $params) {
// Todo: remove excess tags
$params = $this->encodeParams($params);
$this->params = $params;
}
/**
* Returns the url for a flicker api query.
*
* @return string
*/
public function getUrl() {
return $this->url;
}
/**
* Creates the url for a flicker api query.
*
* @return string
*/
public function setUrl() {
$url = '';
$encodedParams = $this->getParams();
if (!empty($encodedParams)) {
$url = 'http://api.flickr.com/services/rest/?' . $encodedParams;
}
$this->url = $url;
}
/**
* Returns the parameters url encoded in a string for querying flickr, or
an empty string if $this->params is empty.
*
* @param array $params
* @return string
*/
protected function encodeParams(array $params) {
$encodedParams = '';
if (!empty($params)) {
$encodedParams = array();
$params['format'] = 'php_serial'; // Add format
$params = array_unique($params);
foreach ($params as $name => $value){
$encodedParams[] = urlencode($name) . '=' . urlencode($value);
}
$encodedParams = implode('&', $encodedParams);
}
return $encodedParams;
}
/**
* Query flicker with a url made up of the parameters set in TypoScript.
*
* @return string
*/
public function queryFlickr() {
$response = file_get_contents($this->getUrl());
return unserialize($response);
}
}
?>
I also have a simple controller at the moment:
<?php
/**
*
*
* @package flickrimages
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public
License, version 3 or later
*
*/
class Tx_Flickrimages_Controller_FlickrController extends
Tx_Extbase_MVC_Controller_ActionController {
/**
* Flickr model
*
* @var Tx_Flickrimages_Domain_Model_Flickr
*/
protected $flickr;
/**
* action list
*
* @return void
*/
public function listAction() {
$photos = $this->flickr->queryFlickr(); // Just for testing; make an image
search by tags from TS
if (is_array($photos)) {
$this->view->assign('photos', $photos['photos']['photo']);
} else {
t3lib_div::debug('$flickrResult is not an array', 'Flickr Result Error');
}
#$rsp = file_get_contents($url);
#$rsp_obj = unserialize($rsp);
}
/**
* Initializes the current action
*
* @return void
*/
public function initializeAction() {
// Create flicker model object
$this->flickr =
t3lib_div::makeInstance('Tx_Flickrimages_Domain_Model_Flickr');
$this->flickr->setParams($this->settings['params']);
$this->flickr->setUrl();
}
}
?>
My questions are:
I created an instance of my model in the initializeAction() function. Is
this the correct thing to do? I found someone else doing this when they also
had an extbase extension without tables (some twitter extension) , I used
the pibase makeinstance function for this, but I think maybe I should be
using some extbase function for this instead? If so which?
Do I need the model if I don't have any repositories? Would it be better to
just do everything in the controller?
The flicker API has many methods for querying their database... I thought I
would just have one controller with a different action for each one... Is
this the correct thing to do, or should I have different contollers for each
one? I thought I shouldn't bother doing this as they are all the same -
query the database and pass the results on to my views.
Thanks for any suggestions/help.
More information about the TYPO3-project-typo3v4mvc
mailing list