• Home
  • About
  • Links
  • Contact
  • Imprint
Blue Orange Green Pink Purple

Using the advantages of Zendframework with Typo3

Posted in Zend Framework, php, typo3. on Wednesday, November 18th, 2009 by Chris Tags: typo3, zend, zendFramework
Nov 18

Today I want to provide two new extensions to you, as well as a little tutorial how to develop Typo3 Extension more MVC like and with a lot less work than you would have usually. But first I’d like to share two nice extensions witch will be the base to this article:

  1. T3X_sl_zendframework-1_9_5-z-200911181108
  2. T3X_slmvchelper-1_0_0-z-200911180116

In one of my previous posts I showed you how to implement the Zend_AutoLoader in your normal PHP project to save your self some time and to make you stop carrying about if a class is loaded all ready or has to be required. Wouldn´t it be nice if one has Autoloading inside Typo3 too ? Well this can be done quite easy. First of all you will have to install the sl_zendframework Extension into your typo3.

This will provide two things to you.

First of all you will be able to use all the nice things the Zendframework delivers at the version 1.9.5 (except zend_locale, zend_gData, and zend_codegenerator those where removed because of size issues). The second advantage you will get is worth even more, you will have lazy loading via autoload.

By default this extension provides the autoloading for all Zend related classes you might need in your extension. The only requirement is that it is installed and loaded before your own extension. You are able to check that in the localconf.php by the order of $TYPO3_CONF_VARS['EXT']['extList']. The sl_zendframework extension has to stand at a position in that list that is before the position of your plugin.

The sl_zendframework will also provide you with the variable $autoloader inside the ext_localconf.php file of your extension witch is a singleton instance of Zend_Loader_Autoloader. This will be the base to get your extension to autoload what is inside,… or even if other extensions use this too, what is inside them too.

The way to achieve this is quite simple:

/**
 * this is the path to typo3conf/ext,
 * or if the extension is installed global typo3/ext
 */
$s_include_path = str_replace('yourExtensionKey/',
                    '',
                    t3lib_extMgm::extPath('yourExtensionKey'));

/**
 * now we add the path to the extensions
 * parent folder to the includepath list
 */
set_include_path(get_include_path() .
                          PATH_SEPARATOR .
                          $s_include_path);

/**
 * now we register the name of the extension as
 * a namespace for the autoloader
 */
$autoloader->registerNamespace ( 'yourExtensionKey' );

After you added those lines to your ext_localconf.php you can almost forget about include and require statements, as long as the class you want to
instanciate is inside your extension folder and prefixed with name of your extension, witch you just registered as a namespace to the autoloader.

Let me show you some examples:

 // file inside  a folder with the name "yourExtensionKey"
 // carrying the name yourClassName.php
 $instance_1 = new yourExtensionKey_yourClassName();

 // file inside  a folder with the name "subFolderName"
 // inside the folder "yourExtensionKey"
 // carrying the name yourClassName.php
 $instance_2 = new yourExtensionKey_subFolderName_yourClassName();

No more require_once needed because the zend_autoloader will do the job for you. Now that I have showed you how to get the autoloader up and running for your extension, let me introduce you to the second extension “slmvchelper”. This extension can be called the result of working with typo3 for several years now (in 2010 it be my 7th anniversary of working with typo3). As I mentioned before I am a little bit lazy when it come to writing code, so I try to save myself as many lines as possible, and try to keep it as simple as possible if someone else or even me myself has to edit a extension I have done. This is way I came up with the idea of the two classes this extension contains (as well as a example pi1) by the start of 2009.

The first one helps you with the template parsing and all the methods one has to use during the development of a extension to get his template parsed and displayed. It is called “slmvchelper_view_templateParser”, and yes if you install the extension on your system it will register it self to the autoloader so that you can use it by this simple line of php:

  $this->view = new slmvchelper_view_templateParser($this);

After that you will have an instance of the Templateparser witch provides you with some simple methods to make your live easier while developing extensions.
But step by step.

Next thing you will have to do is this:

  $this->view->setTemplate($this->conf['template.']['path']);
  $this->view->setSubPart($this->conf['template.']['mainSubPart']);

The “setTemplate” method excepts a string witch leads it to the template file you want to use. In this case this would be provided in plugin.tx_slmvchelper_pi1.template.path and is something like “EXT:slmvchelper/static/html/template.html”. After the call the template
will be loaded. Next thing to do is to set the main Subpart of the template you want to work with. Same here, it is provided in plugin.tx_slmvchelper_pi1.template.mainSubPart and is something like ‘TEMPLATE_MAIN’. The setSubPart method exepts two parameters, the first one is mandatory because it is the subpart you want to declare, the second one is optional because it is the parent subpart of the subpart to declare. So if you want to work on a subpart within another one you would do something like this:

  $this->view->setSubPart('SUBPART_TEST', $this->conf['template.']['mainSubPart']);

Witch would mean you are declaring the subpart named “SUBPART_TEST” inside “TEMPLATE_MAIN”. After you have done this you might want to set some markers and parse the whole stuff and return it. This is also quite easy:

  $marker = array(
		'NORMAL_MARKER' => 'Hallo Welt'
  );

  $subparts = array(
		'SUBPART_TEST' => $this->view->parse($marker, array(),
                                                'SUBPART_TEST')
  );
  $content = $this->view->parse($marker, $subparts,
                                           $this->conf['template.']['mainSubPart']);

By now you might have recognized that those three “#” are missing at the marker name as well as at the subparts name, this is they are no longer required inside your php code, they will be set automatically by the template parser inside the method parse(). I think the first and the second Parameter should be quite obvious, and the third should be no that hard to guess. Right, the third param of the parse method is the name of the subpart you want to use for parsing the values. So for the subpart ‘SUBPART_TEST’ this would be ‘SUBPART_TEST’ and for the template it self this would be $this->conf['template.']['mainSubPart'] or TEMPLATE_MAIN. This is it, to my mind the easiest way to get your template parsed and displayed.

But wait there is more:
The templateparser also provides you with some markers that are there by default. You can access everyting inside your locallang file by simply adding the marker ###LL_yourLocalLangKey### to the template, it will be parsed automatically and add the value of “yourLocalLangKey” to your template, and you do have also every public member variable of $this->cObj->data available by simply adding ###COBJDATA_nameOfTheMember###. So for instance ###COBJDATA_uid### will display the uid of the tt_content element witch is stored inside $this->cObj->data->uid.

The second class of the “slmvchelper” extension is called “slmvchelper_model_base”, witch will provide to you a light version of extBase witch comes with typo3 4.3 this winter. The “slmvchelper_model_base” class gives you the opportunity to simply read and write data to your database. All that is required is for you to set up a model witch has all the fields of your database table as member variables as well as you have to set the table name to work with inside the constructor of the model.

Your model should be looking something like this:

  class yourExtensionKey_model_databaseTableName extends
  slmvchelper_model_base{
	protected $uid;
	protected $pid;
	protected $text;

	public function __construct()
	{
		$this->setTable('tx_yourExtensionKey');
	}

	public function setUid($uid)
	{
		$this->uid = $uid;
	}

	public function getUid()
	{
		return $this->uid;
	}

	public function setPid($pid)
	{
		$this->pid = $pid;
	}

	public function getPid()
	{
		return $this->pid;
	}

	public function setText($text)
	{
		$this->text = $text;
	}

	public function getText()
	{
		return $this->text;
	}
}

Remember we got autoloading so now we can go on in the pi1 for instance with:

  $o_newRecord = new yourExtensionKey_model_databaseTableName();
  $o_newRecord->setText('Hallo Welt');
  $o_newRecord->save();

What just happened is that we inserted a new Record into the database. That simple. After the save() $o_newDummyRecord provides you with $o_newDummyRecord->getUid() witch will return the uid of the record we just inserted.

If you want to read a simple record from the Database the code would look like this:

  $o_newRecord = new yourExtensionKey_model_databaseTableName();
  $o_newRecord->setUid(1);
  $o_newRecord->getThis();

If you want all records that contain the string “Hello” ordered by sorting this would be your code:

  $o_newRecord = new yourExtensionKey_model_databaseTableName();
  $o_newRecord->setText('Hello');
  $result = $o_newRecord->getAll('sorting');

In this case $result would contain an array of yourExtensionKey_model_databaseTableName objects filled with the data of each record.
Even getting querys build like (hidden = 0 AND Deleted = 0 AND (starttime = 0 OR starttime < time()) AND ((endtime = 0 OR endtime < time()))
is possible.

Just like this:

  $o_newRecord = new yourExtensionKey_model_databaseTableName();
  $o_newRecord->setText('Hello');
  $o_newRecord->setHidden(0);
  $o_newRecord->setDeleted(0);
  $o_newRecord->setStarttime('=_0_>=_'.time());
  $o_newRecord->setEndtime('=_0_<=_'.time());
  $result = $o_newDummyRecord->getAll('sorting');

In this case your model should contain the member variables deleted, hidden, starttime and endtime as well as their getters and setters.

All of this you can see in action if you install the sl_zendframework and the slmvchelper extension to your typo3 and put the pi1 of slmvchelper in one of your pages. It will show you a simple example, and if you look inside the pi1 file you can see a little summary of this tutorial working.

I hoped you enjoyed my tutorial and feedback would be appreciated.

9 Comments

  1. Dominique Stender on November 18th, 2009

    I need to read this more in depth… sounds cool.

  2. mstaatz on November 18th, 2009

    Hi Chris,

    nice work!!!
    i will give it a try soon.
    One thing, are you sure that it is allowed, by lthe icence, to remove parts of ZF?

  3. Chris on November 18th, 2009

    Should be no problem to my mind, there is nothing written in the license contributed with the package I downloaded.

    No copyrights etc. are removed, it is still zend framework in the original version, I just dumped the parts one wouldn´t need at all if using ZF with typo3, if you take it as a libary to work with.

  4. mstaatz on November 18th, 2009

    Hi Chris,

    me again, sorry, forgot this hint in my last reply :-)

    if you use in ext_emconf.php the parameter ‘priority’ => ‘top’
    for the sl_zendframework – extension the you are out of trouble. Then default is ‘last’ and every new extension will be loaded after sl_zendframework.

    cheers

  5. Sebastiaan de Jonge on November 18th, 2009

    Nice article Chris, I can really see the resemblance with one of our projects. Was very happy about this way of working!

  6. Chris on November 18th, 2009

    Hi Micha,

    nice hint added it right away, and set the version number to the right version of the zend framework.
    Thanks

  7. Chris on November 18th, 2009

    Thanks Sebastiaan, was very nice working with you on the project at redkiwi.nl. At least I never got beaten up for the framework my weird mind invented by you or Pim ( :-) ) and we both had some fun at the coding. Allthough we had some more crazy stuff in there like the automatic marker generation and the formbuilder,… not to mention the in place editing.

  8. Ebonie Urteaga on November 22nd, 2009

    Wow! What a website. You have a real knack for making a blog readable and easy on the eyes. I find content management systems a very interesting area. I don’t have time to read everything right now, I found your site while I was following up on something else on digg but I have bookmarked the homepage and will come back soon to read more. I have worked with CMS programs for eight years now. Which CMS do you find to be the most easy to customize? I’ve worked with several different applications and they each have their strengths and weaknesses. So many choices…Click here if you’d like to check out my site. Thanks again for a great website.

  9. seotroy on January 23rd, 2010

    very good informationen thank you to you and continues so good ones reports



Leave a Reply

You must be logged in to post a comment.

SwiftLizard Interactive {Design, Development}

  • Last Posts
    • Scrum Master
    • Loging into a page on shell with curl
    • Developing a clock in Flash with AS3
    • Javascript leashed with google Caja
    • Sometimes I wonder about Developers
  • Categories
    • css (1)
    • design (2)
    • development (2)
    • javascript (4)
    • jquery (3)
    • php (4)
    • project management (2)
    • scrum (2)
    • shell (1)
    • thoughts (7)
    • typo3 (3)
    • xhtml (1)
    • Zend Framework (1)
  • Tags
    actionscript 3.0 agile agile development caja continuous integration css design development Flash Flashbuilder javascript jquery Lean OpenAgile performance optimization php product design qunit scrum security shell sql thoughts typo3 xhtml zend zendFramework
  • Friends Blogs
    • Dmitry Dulepov Blog
    • Dominique Stender
    • Gina Steiners Blog
    • Iphone Fan by Dirk Kunde
    • Mario Rimann Blog
    • Michi Zepernick
    • Thomas Hempel :: Typo3 Unleashed
  • Archives
    • March 2010
    • December 2009
    • November 2009
  • Search


  • t3n Social News bookmark at mister wong publish in twitter bookmark at del.icio.us bookmark at digg.com bookmark at furl.net bookmark at linksilo.de bookmark at reddit.com bookmark at spurl.net bookmark at technorati.com bookmark at google.com bookmark at yahoo.com bookmark at facebook.com bookmark at stumbleupon.com bookmark at propeller.com bookmark at newsvine.com bookmark at jumptags.com

RSS Feed


  • Home
  • About
  • Links
  • Contact
  • Imprint

© Copyright SwiftLizard Interactive {Design, Development}. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top