Serializeable behavior for CakePHP

For a project i’m working on i needed to serialize a number of arrays before they could be saved and since i don’t like to serialize and un-serialize all those arrays in each controller action, i wrote a behavior to take care of this.

It serializes all array data before it is saved into the database and when you do a find it will unserialize all data(including related model data).

Download serializeable behavior

Filed Under: CakePHP, Code, English - read on

A central hub for cakePHP

Last week i was in the #CakePHP channel on irc.freenode.net when i noticed a discussion about someone who had created a component, but didn’t want it posted on the bakery. He thought that his code wasn’t up to par with everything else on the bakery. So he thought about posting it on his blog.

I can see why he didn’t want to post it on the bakery. You can get a lot of critics there, but those are only there to help improve the thing you posted and can help you to write better code.

It got me thinking how scattered the information about Cake really is. There is, of course, the bakery, but there are also the saved bookmarks on del.icio.us, the custom search engine, the superfeed and a gazillion blog posts about cake. All containing really good components, helpers and tutorials.

But what i really want is a central hub for all of these great contributions to Cake. Right now i’m feeling that i only get to see a part of all that’s good about cake and still miss out on a lot of good stuff. Shure if i need something i google it first before i create it myself, but there must be a better way to stay up-to-date about all that is cake.

The bakery is a good start, but there are a lot of people who don’t want to post their stuff there, because they think it isn’t good enough and there is a list on the CakePHP home page, but it only lists a few blogs and there are so much more.

I think it would help the community as a whole and give more attention to all the great CakePHP stuff out there.

What do you think? Does CakePHP need a central place for all stuff cake, or do you think it’s fine the way it is. Or maybe you have a great idea to get people to post more of their content to the bakery?
Let me know in the comments!

Filed Under: CakePHP, English - read on

Admin routes

I had some issues today with the admin routes in the new cake version.

The problem was that i was using an admin route in the core (/app/config/core.php)

/**
 * Uncomment the define below to use CakePHP admin routes.
 *
 * The value of the define determines the name of the route
 * and its associated controller actions:
 *
 * 'admin' 		-> admin_index() and /admin/controller/index
 * 'superuser' -> superuser_index() and /superuser/controller/index
 */
Configure::write('Routing.admin', 'dashboard');

And in my routes file (/app/config/routes.php) i was using this:

Router::connect('/dashboard/', array('controller' => 'sites', 'action' => 'index', 'admin'=>'dashboard'));

This didn’t work out as planned, it gave an error about not beeing able to find a controller action (index(); instead of dashboard_index();

Turns out that in the new version you need to use prefix instead of admin, so the line should be:

Router::connect('/dashboard/', array('controller' => 'sites', 'action' => 'index', 'prefix'=>'dashboard'));

Then in my app_controller (/app/app_controller.php) i had a function to switch to the admin layout if it was called in the url, but this changed also.

It used to be:

if (isset($this->params['dashboard'])) {
		$this->layout = 'dashboard';
	}

But i had to change it to:

if (isset($this->params['prefix']) && $this->params['prefix'] == 'dashboard') {
		$this->layout = 'dashboard';
	}

The reason for this is that it uses the prefix value from the routes file.
If you use the first example (the one with $this->params['dashboard']) the dashboard layout will not be set if there is no / after dashboard in the url (http://mysite.com/dashboard), because $this->params['dashboard'] will be empty.
Using the second example it will set the layout.

I also check if the prefix is dashboard, because you could be using more then one prefix.

Filed Under: CakePHP, Code, English - read on

Fancy flashmessage for CakePHP

Flash messages usually need the attention of the people using your site, so why not make them stand out a bit more?

The requirements are: prototype and scriptaculous loaded and highlight.js (i’ll discuss all below)

Download scriptaculous here: http://script.aculo.us/downloads (prototype is included in the zip file)

Upload scriptaculous to your webroot/js

prototype scriptaculous (click for large)

Next we need to create a new javascript file called highlight.js

Insert the following code in there

function highlight_message()
{
	var id="flashMessage";
	if (document.getElementById(id)) {
		var elemento=document.getElementById(id);
		if (elemento.innerHTML.length>0)
			{
				new Effect.Highlight(id, {startcolor:'#FF0033', duration: 1.0} );
			}
	}
	Event.stopObserving(window, 'load', highlight_message);
}
Event.observe(window, 'load', highlight_message, false)

What this script does it waits for the document to load and then fires the highlight_message() function.
This function searches for the id flashMessage. If that id exists on the page it adds a highlight effect from scriptaculous to it.
The startcolor is reddish, but you can change it to any hex colour (like #FFF or #000).
Upload this file to the /webroot/js directory too.

Now load the javascript files in your default.ctp

<?php print $javascript->link('prototype') ?>
	<?php print $javascript->link('scriptaculous') ?>
	<?php print $javascript->link('highlight') ?>

And there you have it, an unobtrusive highlight function that highlights the flashmessage.

Filed Under: CakePHP, Code, English - read on

Flay and cakePHP

Whilst browsing the CakePHP API documentation this morning i stumbled upon the class Flay. According to the documentation it’s a textformatter like Textile.
This got me interested and i tried searching for some documentation about flay in cake. Unfortunately i couldn’t find anything on the net. So i dove into the code and tried some tags to see what it does.

To use flay you can either use the Texthelper

In your controller do:

var $helpers = array('Text');

And in your view do:

flay($text); ?>

You can allow html tags by using this code instead of the above (there are some issues with this tough, see bottom of article):

<?php echo $text->flay($text, true); ?>

You can also use the uses(’Flay’); in your controller/model

uses('flay');
class <ClassName> extends AppModel {...}

This way you get some extra features to use within the Flay class like:

colorMark($words, $string)
extractWords($string)
fragment($text, $length, $ellipsis= '...')
log($msg, $type=LOG_ERROR)
markedSnippets($words, $string, $max_snippets=5)
requestAction($url, $extra=array())
toClean($text)
toHtml($text=null, $bare=false, $allowHtml=false)
toParsedAndClean($text)
toString()

See more about the other features at docs.cakephp.nu

It has a few nice features to make your submitted text a little nicer.

First of it replaces the double newline with a paragraph, saving you the typing of

<p> and </p>

To force a paragraph you could also type

%PARAGRAPH%

For a linebreak type:

%LINEBREAK%

You can also make blockquotes:

%BLOCKQUOTE% ... %ENDBLOCKQUOTE%

Or center the text with

%CENTER% ... %ENDCENTER%

To format the text strong/bold or emphasis/italic use

*strong* or _emphasis_

It also does some basic url detection/formatting

links beginning with http:// are automatically formatted to a link, so no more

<a href=""></a>

To force a link type

[link]

where link is the actual url.

Images are also automatically detected so instead of

<img src="http://url.com/image.jpg"/>

type

http://url.com/image.jpg

Same goes with E-mail adresses.

These are the basic features of Flay and for some basic html formatting it’s really nice to use. But if you want to add a title to your link for example, or use alt text for your image you need to use the html tags.

And that’s where the problem is. Flay breaks html code, because it replaces the < and > tags. So a

<a href="cakephp.org" title="link to cakephp">CakePHP</a>

becomes

<a href="<a href="cakephp.org"">cakephp.org"</a> title="link to cakephp">CakePHP</a>

wich obviously is broken.

For simple formatting Flay does the job well and for most users of simple sites it’s sufficient. But if you want more semantic html (alt-tags and titles) you have to use one of the other formatting tools like Textile

Filed Under: CakePHP, Code, English - read on