[TYPO3-mvc] Possibility to let Extbase-Ext return an array?

Bastian Waidelich bastian at typo3.org
Thu Jun 18 21:35:05 CEST 2009


Joerg Schoppet wrote:

Hi Joerg,

> So all the possible different outputs are only different views in a MVC 
> approach, e.g.:
> - html-output (actually)
> - csv-output
> - xml-output
> - json-output
> - php-array-output
> - ...

You couldn't output PHP in your view because it would be parsed on the 
server. Sure, you could have something like a "serialized php array" 
view but IMO that's like breaking a fly on the wheel.

What you actually need (If I got it right) is an API. In your case, an 
API that allows you to fetch products or categories (or products by 
category etc).
Luckily your model already provides such API.

I've just tried it out by creating a 2nd level menu with all available 
Blogs in the blog_example.
If you want to try it out yourself, just follow this little mini-tutorial (:

1. create a class "class.tx_blogexample_menuitem_procfuncs.php" with 
following content:

<?php
class tx_blogexample_menuitem_procfuncs {

	public function createBlogMenuItems($menuArr, $conf) {
		$this->blogRepository = 
t3lib_div::makeInstance('Tx_BlogExample_Domain_Model_BlogRepository');
		$blogs = $this->blogRepository->findAll();
		foreach ($blogs as $blog) {
			$queryParams = '&tx_blogexample_pi1[blog][uid]=' . 
(integer)$blog->getUid() . 
'&tx_blogexample_pi1[action]=index&tx_blogexample_pi1[controller]=Post';
			$queryParams.= 
'&cHash='.t3lib_div::shortMD5(serialize(t3lib_div::cHashParams($queryParams)));
			$item = array(
				'uid' => $conf['targetPid'],
				'title' => $blog->getName(),
				'_ADD_GETVARS' => $queryParams,
				$item['ITEM_STATE'] = 'NO'
			);
			$menuArr[] = $item;
		}
		
		return $menuArr;
	}
}
?>

2. Add following TS to your menu setup:

includeLibs.tx_blogexample_menuitem_procfuncs = 
EXT:blog_example/class.tx_blogexample_menuitem_procfuncs.php

lib.myMenu = HMENU
lib.myMenu {
	1 = TMENU
	1 {
		wrap = <ul> | </ul>
		NO.wrapItemAndSub = <li> | </li>
	}
	2 < .1
	2 {
		itemArrayProcFunc = tx_blogexample_menuitem_procfuncs->createBlogMenuItems
		itemArrayProcFunc {
			targetPid = {$blogIndexPid}
		}
	}
}

3. set blogIndexPid to a uid containing the blog_example in your TS 
constants

ready is your fancy cached Blog-menu ;)

BTW: You can't name the userFunc like "Tx_BlogExample_...", because 
tslib_cObj:callUserFunction() checks, whether the class starts with 
"tx".. lowercased - that should be fixed in TYPO3 core IMHO..

Bastian


More information about the TYPO3-project-typo3v4mvc mailing list