<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sava&#039;s Place &#187; CakePHP</title>
	<atom:link href="http://savasplace.com/category/programming/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://savasplace.com</link>
	<description>Free PHP scripts, CSS templates, Making Money Online and Miscellaneous Ramblings</description>
	<lastBuildDate>Tue, 07 Feb 2012 07:33:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Multiple CSS Style Session Flash&#8217;es in CakePHP</title>
		<link>http://savasplace.com/2009/08/multiple-css-style-session-flashes-in-cakephp/</link>
		<comments>http://savasplace.com/2009/08/multiple-css-style-session-flashes-in-cakephp/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 08:34:08 +0000</pubDate>
		<dc:creator>Sava</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[session flash]]></category>

		<guid isPermaLink="false">http://savasplace.com/?p=3904</guid>
		<description><![CDATA[We all know that CakePHP has the Session component that allows us to login and logout users and to show them messages that appear only once: &#60;?php $this->Session->setFlash('Random message that appears only once'); ?&#62; We can style this message the way we want but what about having these kind of messages that are style differently [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://savasplace.com/wp-content/uploads/2009/05/cakephp-150x150.png" alt="CakePHP" title="CakePHP" class="alignleft" />We all know that CakePHP has the Session component that allows us to login and logout users and to show them messages that appear only once:</p>
<p><code>&lt;?php $this->Session->setFlash('Random message that appears only once'); ?&gt;</code></p>
<p>We can style this message the way we want but what about having these kind of messages that are style differently depending on what kind of messages we show (error, success, warning or general messages ).</p>
<p>The easy way to do it is to create our own helper.  So let&#8217;s go to <strong>app/views/helpers</strong> folder and create the helper.  Create a new file and rename it to <strong>flash.php</strong> and put the following code in it:</p>
<p><code><br />
&lt;?php<br />
class FlashHelper extends Helper {</p>
<p>	var $helpers = array('Session');</p>
<p>	function show() {<br />
		$messages = $this-&gt;Session-&gt;read('messages');<br />
		$html = '&lt;ul class="system_messages"&gt;';<br />
		if($messages) {<br />
		foreach ($messages as $type =&gt; $msgs) {<br />
			foreach ($msgs as $msg) {<br />
				if (!empty($msg)) {<br />
					$html .= "&lt;li class='$type'&gt;&lt;span class='ico'&gt;&lt;/span&gt;&lt;strong class='system_title'&gt;$msg&lt;/strong&gt;&lt;/li&gt;";<br />
				}<br />
			}<br />
		}<br />
		$html .= "&lt;/ul&gt;";<br />
		$this-&gt;Session-&gt;del('messages');<br />
		return $this-&gt;output( $html );<br />
		}<br />
	}<br />
}<br />
?&gt;<br />
</code></p>
<p>As you can see the show() function read the messages stored in the session. Each read message is written back inside a <strong>&lt;li&gt;</strong> tag that has a different class <strong>$type</strong>.</p>
<p>Now go ahead and edit your <strong>app_controller.php</strong> file located in the <strong>app</strong> folder.<br />
If you already have the helpers array written in it then add the <strong>Flash</strong> helper to the array. If not then put this code in the file:</p>
<p><code><br />
var $helpers = array('Flash');<br />
</code></p>
<p>Now add the function below to the <strong>app_controller.php</strong> as well:</p>
<p><code><br />
	function flash( $message, $class = 'status' ) {<br />
		$old = $this->Session->read('messages');<br />
		$old[$class][] = $message;<br />
		$this->Session->write('messages', $old );<br />
	}<br />
</code></p>
<p>Create different css styles for the types of message you want to display (error, success, warning or general messages).</p>
<p>Now to add a flash message you have to use the <strong>flash helper</strong> instead of the session flash component.<br />
So &#8230; instead of:</p>
<p><code><br />
$this->Session->setFlash('A cool message that is a warning!');<br />
</code></p>
<p>you will use :</p>
<p><code><br />
$this->flash('A cool message that is a warning!', 'warning');<br />
</code></p>
<h3>My Example</h3>
<p>I created the following CSS styles: white, red, green, blue, yellow.</p>
<p>Now all I have to do is this:</p>
<p><code><br />
		$this->flash('This is a simple notification message', 'white');<br />
		$this->flash('This is an error message', 'red');<br />
		$this->flash('This is a successful message', 'green');<br />
		$this->flash('This is a blue message <img src='http://savasplace.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ', 'blue');<br />
		$this->flash('This is a warning message', 'yellow');<br />
</code></p>
<p>inside a function and here&#8217;s the result:</p>
<p><img src="http://savasplace.com/wp-content/uploads/2009/08/cakephp-example-of-different-css-styled-session-flash-messages.jpg" alt="CakePHP example of different css styled session flash messages" title="CakePHP example of different css styled session flash messages" width="400" height="280" class="alignnone size-full wp-image-3909" /></p>
<p>Hope you find this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://savasplace.com/2009/08/multiple-css-style-session-flashes-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Multilingual Website with CakePHP</title>
		<link>http://savasplace.com/2009/08/multilingual-website-with-cakephp/</link>
		<comments>http://savasplace.com/2009/08/multilingual-website-with-cakephp/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 07:53:40 +0000</pubDate>
		<dc:creator>Sava</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[multi language website with cakephp]]></category>
		<category><![CDATA[Multilingual]]></category>

		<guid isPermaLink="false">http://savasplace.com/?p=2606</guid>
		<description><![CDATA[We all know that developing a website in CakePHP is very easy and also fast. Here&#8217;s how to create a multilingual website fast. First open app/config/bootstrap.php and set the languages you want available for your website: Configure::write('Config.languages', array( 'ro' => array( 'language' => 'Romanian', 'locale' => 'rum', 'localeFallback' => 'rum', 'charset' => 'utf-8' ), 'en' [...]]]></description>
			<content:encoded><![CDATA[<p>We all know that developing a website in CakePHP is very easy and also fast. Here&#8217;s how to create a multilingual website fast.</p>
<p>First open <strong>app/config/bootstrap.php</strong> and set the languages you want available for your website:</p>
<p><code><br />
Configure::write('Config.languages', array(<br />
	  	'ro' => array(<br />
		    'language' => 'Romanian',<br />
		    'locale' => 'rum',<br />
		    'localeFallback' => 'rum',<br />
		    'charset' => 'utf-8'<br />
		),<br />
		'en' => array(<br />
		    'language' => 'English',<br />
		    'locale' => 'eng',<br />
		    'localeFallback' => 'eng',<br />
		    'charset' => 'utf-8'<br />
		),<br />
	)<br />
);<br />
</code></p>
<p>I chose english and romanian for this example.</p>
<p>Now open your app controller (<strong>app/app_controller.php</strong> &#8211; create one if you don&#8217;t have it) and put this function in it:</p>
<p><code><br />
	function setLanguage() {<br />
		if(!isset($this->params['lang'])) $this->params['lang'] = 'ro';<br />
		$lang = $this->params['lang'];<br />
		App::import('Core', 'i18n');<br />
		$I18n =&#038; I18n::getInstance();<br />
		$I18n->l10n->get($lang);<br />
		foreach (Configure::read('Config.languages') as $lang => $locale) {<br />
			if($lang == $this->params['lang'])<br />
				$this->params['locale'] = $locale['locale'];<br />
		}<br />
	}<br />
</code></p>
<p>All you have to modify in this function is the first line (that sets the default language). Change the default language to your chosen one. </p>
<p>It&#8217;s now time to create a route depending on the language. Open <strong>app/config/routes.php</strong> and add this route to it:</p>
<p><code><br />
Router::connect('/:lang/:controller/:action/*', array('lang' => 'ro'), array('lang' => 'ro|en'));<br />
</code></p>
<p>Of course that this can change depending on the languages you are using and their number. You&#8217;re a smart boy so you&#8217;ll figure it out.</p>
<p>Now all you have left is to create the language files. Go to <strong>app/locale</strong> and create 2 folders: <strong>rum</strong> and <strong>en</strong>. In each of these folders create another folder called <strong>LC_MESSAGES</strong>. In the LC_MESSAGES folders you will now store the language files. Language files can be divided so it&#8217;s easier for you to store the translations ( <strong>.po</strong> files ).</p>
<p>For example, you can create a <strong>login.po</strong>, <strong>register.po</strong>, <strong>account.po</strong> and <strong>default.po</strong>.</p>
<p>In the language files you have to set the message id and message string.</p>
<p><em><strong>English</strong></em><br />
<code><br />
msgid "hello"<br />
msgstr "Hello"<br />
</code></p>
<p><em><strong>Romanian</strong></em><br />
<code><br />
msgid "hello"<br />
msgstr "Buna ziua"<br />
</code></p>
<p>And now to echo these strings on your website you have to remember the string id and the name of the language file (.po) it is stored in. Example:</p>
<p><code><br />
&lt;?<br />
__d('default', 'hello', true);<br />
?&gt;<br />
</code></p>
<p>This will take the string with the id &#8220;hello&#8221; from the <strong>default.po</strong> file and echo it. And true means that it will echo the string.<br />
Good luck</p>
]]></content:encoded>
			<wfw:commentRss>http://savasplace.com/2009/08/multilingual-website-with-cakephp/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Multiple Databases in CakePHP</title>
		<link>http://savasplace.com/2009/05/multiple-databases-in-cakephp/</link>
		<comments>http://savasplace.com/2009/05/multiple-databases-in-cakephp/#comments</comments>
		<pubDate>Mon, 04 May 2009 16:59:39 +0000</pubDate>
		<dc:creator>Sava</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[multiple databases]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://savasplace.com/?p=2543</guid>
		<description><![CDATA[There comes a time in life when you need to use multiple databases for a website you have to build . I came across this &#8220;problem&#8221; when I was working on a portal website and I had to make sure that users can login with the same details on any website that company owned. So [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://savasplace.com/wp-content/uploads/2009/05/cakephp.png" alt="CakePHP" title="CakePHP" width="180" height="180" class="alignleft" />There comes a time in life when you need to use multiple databases for a website you have to build <img src='http://savasplace.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .<br />
I came across this &#8220;problem&#8221; when I was working on a portal website and I had to make sure that users can login with the same details on any website that company owned.</p>
<p>So I had to create a common database in which to store the users. Since all the websites were built in CakePHP I had to find a way to make it connect to other databases besides the default one.</p>
<p>If you are familiar with CakePHP you know that the database details are kept in <strong>app/config/database.php</strong>. </p>
<p>The default database array is:</p>
<p><code><br />
	var $default = array(<br />
		'driver' => 'mysql',<br />
		'persistent' => false,<br />
		'host' => 'localhost',<br />
		'login' => 'mysqlusername',<br />
		'password' => 'mysqlpassword',<br />
		'database' => 'mysqldatabase',<br />
		'prefix' => ''<br />
	);<br />
</code></p>
<p>Since you&#8217;re here now add another database array:</p>
<p><code><br />
	var $database2 = array(<br />
		'driver' => 'mysql',<br />
		'persistent' => false,<br />
		'host' => 'localhost',<br />
		'login' => 'mysqlusername2',<br />
		'password' => 'mysqlpassword2',<br />
		'database' => 'mysqldatabase2',<br />
		'prefix' => ''<br />
	);<br />
</code></p>
<p>Now open up a model you are using. Let&#8217;s say we are using the model <strong>User</strong> ( <em>located in app/models/user.php</em> ). Open the file and add this function to it:</p>
<p><code><br />
	function changeDataSource($newSource) {<br />
		parent::setDataSource($newSource);<br />
		parent::__construct();<br />
	}<br />
</code></p>
<p>Now in the <strong>Users controller</strong> ( <em>located in app/controllers/users_controller.php</em> ) add this function:</p>
<p><code><br />
	function changeDbSource($database = 'default') {<br />
		$db = ConnectionManager::getInstance();<br />
		$connected = $db->getDataSource($database);<br />
		if($connected->isConnected()) {<br />
			return true;<br />
		} else {<br />
			return false;<br />
		}<br />
	}<br />
</code></p>
<p>Here&#8217;s an example on how to switch between the databases:</p>
<p><code><br />
function loginUser() {<br />
// The search is made in the default database<br />
$this->User->find('all', array('conditions' => array('User.username' => $username)));</p>
<p>// Let's do something in the second database <strong>database2</strong></p>
<p>$this->User->changeDataSource('database2');<br />
if($this->changeDbSource('database2')) {<br />
     // Do something in database 2<br />
}<br />
}<br />
</code></p>
<p>It&#8217;s really easy once you get the hang of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://savasplace.com/2009/05/multiple-databases-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

