<?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>Redmonster &#187; PHP</title>
	<atom:link href="http://www.goredmonster.com/topics/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.goredmonster.com</link>
	<description>Agile Web Development</description>
	<lastBuildDate>Thu, 09 Jun 2011 21:09:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Five Great Resources for Beginning PHP Programmers</title>
		<link>http://www.goredmonster.com/php/five-great-resources-for-beginning-php-programmers/</link>
		<comments>http://www.goredmonster.com/php/five-great-resources-for-beginning-php-programmers/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 15:00:57 +0000</pubDate>
		<dc:creator>Paul Redmond</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.goredmonster.com/?p=421</guid>
		<description><![CDATA[Build Your Own Database Driven Web Site Using PHP &#038; MySQL Author Kevin Yank is a seasoned writer, who delivers a masterful explanation of how to program with PHP, in a tutorial-style format. The book is easy to follow and is specifically written for beginners. Sitepoint books are known in the community for their paramount [...]]]></description>
			<content:encoded><![CDATA[<h3>Build Your Own Database Driven Web Site Using PHP &#038; MySQL</h3>
<p><img src="http://www.goredmonster.com/wp/wp-content/uploads/2010/11/database_driven_php_mysql1.jpg" alt="" title="database_driven_php_mysql" width="161" height="208" class="alignleft size-full wp-image-444" /> Author Kevin Yank is a seasoned writer, who delivers a masterful explanation of <a href="http://www.sitepoint.com/books/phpmysql4/">how to program with PHP</a>, in a tutorial-style format. The book is easy to follow and is specifically written for beginners. Sitepoint books are known in the community for their paramount quality of books on web technologies. This book will help you grasp core fundamentals of the PHP language and MySQL. <a href="http://articles.sitepoint.com/article/php-mysql-tutorial">Check out Kevin Yank&#8217;s article</a> related to the newest edition of the book, with four tutorials.</p>
<h3>PHP Solutions: Dynamic Web Design Made Easy</h3>
<p><img src="http://www.goredmonster.com/wp/wp-content/uploads/2010/11/php_solutions-150x150.jpg" alt="" title="php_solutions" width="150" height="150" class="alignright size-thumbnail wp-image-440" /><a href="http://foundationphp.com/phpsolutions/">PHP solutions</a> is a wonderful resource for beginners, and I personally find the PHP installation section invaluable for beginners. If you&#8217;re on Windows, this book walks through the process of setting up the PHP environment from source and configuring Apache.</p>
<p>David focuses on PHP as a language without introducing database logic until later in the book &#8211; which helps the reader focus on PHP without even worrying about MySQL. Readers will also gain important insight on how forms are processed, manipulating images, creating a dynamic photo gallery, and a number of core concepts beginners need to learn on the way to mastering PHP.</p>
<h3>Zend &#8211; The PHP Company</h3>
<p>Zend is the keystone to the PHP community in many regards. The <a href="http://devzone.zend.com/node/view/id/627">PHP 101 articles</a> are great beginner tutorials cover the most essential parts of learning PHP. Become familiar with the <a href="http://devzone.zend.com/public/view">Zend developer zone</a>.</p>
<h3>PHP Manual</h3>
<p>This resources is an obvious one, but keep the PHP manual handy. Firefox has a <a href="http://mycroft.mozdev.org/search-engines.html?name=php.net">search engine plugin for the PHP manual</a> which makes it really easy to search, and in Google Chrome you can type &#8220;php.net&#8221; + space + function and Chrome will automatically take you to search results.</p>
<p>Be cautious of the user examples on php.net; some are useful, but don&#8217;t copy code blindly from commenters without learning exactly what the code is doing. There are code examples which are part of the official documentation &#8211; these are separate from user comments.</p>
<h3>Online Forums</h3>
<p>Online forums are not the fastest way to find what you are looking for if you need help; but if you don&#8217;t personally know anyone familiar with PHP, forums are a great way to ask questions to experienced developers. I particularly like <a href="http://www.sitepoint.com/forums/forumdisplay.php?f=34">Sitepoint&#8217;s PHP forum</a> and <a href="http://forums.devshed.com/php-development-5/">devshed</a>.</p>
<h3>Next Steps</h3>
<p>Once you&#8217;ve mastered the essentials of PHP, you need to learn about Object Oriented Programming. David Power&#8217;s <a href="http://foundationphp.com/pos/">PHP Object Oriented Solutions</a> is a perfect companion to the aforementioned PHP Solutions.</p>

<!-- Start TwitterButton -->
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><div class="twitter-button"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="http://www.goredmonster.com/php/five-great-resources-for-beginning-php-programmers/" data-via="goredmonster" data-text="Five Great Resources for Beginning PHP Programmers">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.goredmonster.com/php/five-great-resources-for-beginning-php-programmers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serious PHP debugging With XDebug</title>
		<link>http://www.goredmonster.com/php/serious-php-debugging-with-xdebug/</link>
		<comments>http://www.goredmonster.com/php/serious-php-debugging-with-xdebug/#comments</comments>
		<pubDate>Sun, 09 May 2010 04:58:28 +0000</pubDate>
		<dc:creator>Paul Redmond</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.goredmonster.com/?p=333</guid>
		<description><![CDATA[Any serious PHP developer should use a debugging tool. Applications are becoming more complex, using OOP, and spanning more files. This post will quickly bring you up to speed and give you the most awesome debugging tools on the planet. I have been using Zend Server CE for local development. It has all the bells [...]]]></description>
			<content:encoded><![CDATA[<p>Any serious PHP developer should use a debugging tool. Applications are becoming more complex, using OOP, and spanning more files. This post will quickly bring you up to speed and give you the most awesome debugging tools on the planet.</p>
<p>I have been using <a href="http://www.zend.com/en/products/server-ce/">Zend Server CE</a> for local development. It has all the bells and whistles you need, and you can pick from 5.2 or 5.3. Zend Server CE comes with the Zend debugger, but I prefer to use XDebug. You can follow <a href="http://www.danceric.net/2009/05/09/zend-server-community-edition-on-os-x/">this tutorial</a> to get XDebug working with Zend Server CE on OS X.</p>
<p>Once you have XDebug installed, you should see it listed in PHP info by calling</p>
<pre>&lt;?php phpinfo(); ?&gt;</pre>
<p> in a PHP script or
<pre>php -i</pre>
<p> from the command line.</p>
<h2>Configuring XDebug</h2>
<p>Once you have XDebug working and you have restarted Apache, you need to add a few things to your php.ini file if you want to actually step through your code line-by-line. If you are using Zend Server CE, it is probably located at
<pre>/usr/local/zend/etc/php.ini</pre>
<p>Add the following lines to your php.ini file under zend_extension (add lines in bold):</p>
<pre>[xdebug]
zend_extension="/usr/local/zend/lib/php_extensions/xdebug.so"
<strong>xdebug.remote_enable=On</strong>
<strong>xdebug.remote_autostart=On</strong></pre>
<p>Restart Apache again, and you can double-check php info to see the changes to <strong>remote_enable</strong> and <strong>remote_autostart</strong> were set correctly.</p>
<p>These php.ini settings allow debugging to start automatically in an external application, as you will see below&#8230;</p>
<h2>MacGDBp &#8211; Debugging Software for XDebug</h2>
<p>Users of OS X can install a program called <a href="https://www.bluestatic.org/software/macgdbp/">MacGDBp</a> to step through PHP code and debug variables. This application intercepts browser requests made to your application, and allows you to set breakpoints and debug your code much faster.</p>
<p>Windows users have a couple software options for xDebug, such as Ultraedit Studio, Komodo Edit, Aptana (although I have never got it working correctly with Aptana), Eclipse, et al.</p>
<p>With MacGDBp open, load a page from your web browser in a PHP application hosted on localhost, and your browser will look like it&#8217;s hanging (if you set up xDebug correctly). You should see the first part of your application code in MacGDP.</p>
<p>You can also add breakpoints by browsing to files with the breakpoint window. In MacGDBp, open Breakpoints by going to Window > Breakpoints or Shift + Command + B. Click the plus button to browse through your files.</p>
<p>Once the file appears in the breakpoints window, you can click line numbers to add breakpoints. Breakpoints will appear as blue arrows. At the time of this writing, clicking the correct line number was somewhat difficult for me, but it just takes some getting used to. The application will halt execution at your breakpoints after you click &#8220;Run&#8221; in MacGDBp.</p>
<p><img src="http://www.goredmonster.com/wp/wp-content/uploads/2010/05/MacGDBp_breakpoints.png" alt="MacGDBp_breakpoints" title="MacGDBp_breakpoints" width="578" height="510" class="alignnone size-full wp-image-343" /></p>
<p>Assuming the file you add is loaded during the page request, if you click &#8220;Run&#8221;, MacGDBp will let the browser request resume until it hits your breakpoint. From here, you can step in/out/over of functions. You can also browse variables and make sure they are being set properly.</p>
<p>If you need to step through the code again, click the &#8220;Reconnect&#8221; button on the top right in MacGDBp, and click refresh in your browser. You can also leave MacGDBp disconnected and develop your application normally until you need to debug again. When you reconnect macGDBp, it will then stop at the first line of code, allowing you to step through again or run to your next breakpoint.</p>
<p><img src="http://www.goredmonster.com/wp/wp-content/uploads/2010/05/MacGDBp_step.png" alt="MacGDBp_step" title="MacGDBp_step" width="769" height="561" class="alignnone size-full wp-image-350" /></p>
<h2>Step In, Step Out, Step Over</h2>
<p>When you start debugging a request with MacGDBp, you will be presented with the first executable line of code. At this point, you can determine how to proceed. If the line is a function, you can click &#8220;Step In&#8221; and MacGDBp will take you to the function being called.</p>
<p>While in that function, you will step through that function line-by-line. You can continue to step through functions called within other functions, which can get you lost in a hurry if you&#8217;re not careful. If you want to stay inside the current function, you can click &#8220;step over&#8221; at a function to go to the next line.</p>
<p>Stepping out will bring you back to the original code where you stepped into the current function originally. This is helpful if you accidentally step into a function and you want to go back.</p>
<h2> Modifying UI</h2>
<p>I recommend modifying the icons in MacGDBp, to include Icons and text. You can do this by right-clicking in the gray icon area of the application. You can select Icons and text, which makes it easy to understand the icons and what you are doing until you become a seasoned debugger.</p>
<p><img src="http://www.goredmonster.com/wp/wp-content/uploads/2010/05/icons_text.png" alt="icons_text" title="icons_text" width="563" height="344" class="alignnone size-full wp-image-351" /></p>

<!-- Start TwitterButton -->
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><div class="twitter-button"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="http://www.goredmonster.com/php/serious-php-debugging-with-xdebug/" data-via="goredmonster" data-text="Serious PHP debugging With XDebug">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.goredmonster.com/php/serious-php-debugging-with-xdebug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to CakePHP for Designers: Part II</title>
		<link>http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-designers-part-ii/</link>
		<comments>http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-designers-part-ii/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 06:49:07 +0000</pubDate>
		<dc:creator>Paul Redmond</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.goredmonster.com/?p=191</guid>
		<description><![CDATA[In the first tutorial, I covered basic foundations of views, and set out create a simple task management application. This tutorial will focus on controllers, which interact with both views and models. The process of the MVC framework is a simple paradigm, where controllers handle requests, get data from the model as needed, and process [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-web-designers/">first tutorial</a>, I covered basic foundations of views, and set out create a simple task management application. This tutorial will focus on <strong>controllers</strong>, which interact with both views and models. The process of the MVC framework is a simple paradigm, where controllers handle requests, get data from the model as needed, and process the request by giving feedback to the user through views. Controllers play an important middle-man role in MVC applications.</p>
<p>To help illustrate the role controllers play, consider a bank. Customers request information from the bank through ATMs, online banking websites, and bank employees. Customers don’t have direct access to the bank’s safe, they must request this information from ATMs and bank tellers, etc. ATMs and tellers represent the controller in our analogy. They facilitate the requests of customers by accessing account information and money. This information is then processed and returned to the customer. In the same way, users of an MVC application make requests to the controller through views. These requests are received by the controller. The controller requests data from the database through models. Controllers then return data to views.</p>
<p>CakePHP controllers work closely with models to facilitate data from the database. This means we need to create a database and a table to get our controller working. We could bypass the model (controllers don&#8217;t technically need models in some cases) and just use our controller, but that would only be confusing to those learning the importance of controllers in working with both models and views.</p>
<p>Lets start by creating a database. For simplicity, lets name the database “taskmaster” (I know, totally cheezy :-/). You should have a working MySQL database server running. This tutorial will not go into the details of setting up MySQL and PHP. Fire up your favorite MySQL management tool, or use the handy terminal if that suits you. I’m going to type the following to login via the terminal:</p>
<pre>mysql -u root -p</pre>
<p>MySQL will prompt you for a password. Once you type in your password (default is blank, or no password) you will see the mysql prompt. Type in:</p>
<pre>create database taskmaster;</pre>
<p>Don’t forget the semi-colon! Verify that the database was created by typing in:</p>
<pre>show databases</pre>
<p>Now that we have our database created, lets create a table. Enter the following query to create your first table.</p>
<pre>
CREATE TABLE `tasks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `description` varchar(255) NOT NULL,
  `completed` tinyint(1) DEFAULT '0',
  `created` datetime,
  `modified` datetime,
  PRIMARY KEY (`id`)
);
</pre>
<p>This might seem like a simple table, but there are a few important conventions that CakePHP will reward. The table name “tasks” will ensure that we can create a controller called “TasksController” and a model called “Task”. We don’t have to do much configuration to start working with our data if we use these naming conventions. The lowercase id field is standard on any table in CakePHP. Created and modified columns will be automatically updated by CakePHP when we save a new record or update one. We don’t even have to worry about these columns when saving data.</p>
<p>Lets create a few records so we have actual data to retrieve from our model via the controller. Run the following queries to get some tasks in the tasks table:</p>
<pre>
INSERT INTO tasks (description, created, modified)
    VALUES ('Take out the trash.', NOW(), NOW());
INSERT INTO tasks (description, created, modified)
    VALUES ('Wash the car.', NOW(), NOW());
</pre>
<p><em>Note: I added NOW() twice to simulate what CakePHP will do when we create a new task within the application.</em></p>
<p>Now that our table is created and has a few tasks entered, lets jump over to our CakePHP application. The first order of business is telling CakePHP about our database. Navigate to app/config/database.php and change the default connection to match your database information. Mine looks like the following:</p>
<pre>
var $default = array(
	'driver' => 'mysql',
	'persistent' => false,
	'host' => 'localhost',
	'login' => 'root',
	'password' => '',
	'database' => 'taskmaster',
	'prefix' => '',
);
</pre>
<p>Create a new file in app/controllers called tasks_controller.php and populate with the following code:</p>
<pre>
&lt;?php
class TasksController extends AppController {
	public $name = ‘Tasks’;

	public function index() {

	}
}
</pre>
<p>I’ve added an empty function for our default action. As you will see shortly, functions in controllers are actions requested by users through a URL. Index is the default action for any controller unless you specify otherwise in app/config/routes.php. Controller class names are camel-cased, and the file names use underscores, which is why TasksController’s filename is tasks_controller.php. Controllers are generally named the same as the name of the database table.</p>
<p>Now we need to create a model file. Create a new file in app/models called task.php and populate with the following:</p>
<pre>
&lt;?php
class Task extends AppModel {
	public $name = ‘Task’;
}
</pre>
<p>We now have a skeleton model and controller. Since we have data, lets write our controller code to get our tasks from the database and then display them in the view. Our controller will request tasks from the tasks table, and for now, we will request all tasks (we only have two so far after all). Your tasks controller will look like this:</p>
<pre>
&lt;?php
public function index() {
	$tasks = $this->Task->find('all');
	$this->set('tasks', $tasks);
}
</pre>
<p>We are getting all tasks in the database, using the Task model. $this->Task represents the model we just created. Both our controllers and models inherit a bunch of built-in functionality from AppController and AppModel. <br />$this->Task->find() is one such method. The controller then sets a variable for the view called “tasks” and is populated with an associative array that was returned from the find method. We have everything we need to display model data in the view&#8230;let’s get going!</p>
<p>Create a folder in app/views called tasks. This folder name also matches the controller name, which keeps our views organized. In the newly created folder, create a file called index.ctp, which matches our index function in TasksController, and paste the following:</p>
<pre>
&lt;h1&gt;Tasks&lt;/h1&gt;
&lt;table&gt;
	&lt;thead&gt;
		&lt;tr&gt;
			&lt;th scope="col"&gt;Id&lt;/th&gt;
			&lt;th scope="col"&gt;Description&lt;/th&gt;
			&lt;th scope="col"&gt;Complete?&lt;/th&gt;
			&lt;th scope="col"&gt;Created&lt;/th&gt;
			&lt;th scope="col"&gt;Modified&lt;/th&gt;
		&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
	&lt;?php foreach($tasks as $task):
		$completed = $task['Task']['completed'] == FALSE ? 'No' : 'Yes';
	?&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;?php echo $task['Task']['id']; ?&gt;&lt;/td&gt;
			&lt;td&gt;&lt;?php echo $task['Task']['description']; ?&gt;&lt;/td&gt;
			&lt;td&gt;&lt;?php echo $completed; ?&gt; &lt;/td&gt;
			&lt;td&gt;&lt;?php echo $task['Task']['created']; ?&gt;&lt;/td&gt;
			&lt;td&gt;&lt;?php echo $task['Task']['modified']; ?&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;?php endforeach; ?&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
</pre>
<p>You can now see the index action of our controller by navigating to the url of your application. Mine is <strong>http://localhost/cakephp-designers/tasks</strong>. The url contains our controller name &#8220;tasks&#8221;. You can omit the &#8220;index&#8221; action from the url because, like I mentioned above, index is the default action.</p>
<p>To illustrate how URLs match functions in a controller, you can navigate to http://localhost/cakephp-designers/tasks/index and get the same page. An add action might have this URL: http://localhost/cakephp-designers/tasks/add and would call a function called &#8220;add&#8221; in the TasksController class.</p>
<p>Our plain vanilla table has a foreach loop that loops through the data supplied from the controller that was assigned to a variable called tasks. It is important to understand the array structure that CakePHP returns when retrieving data from models. You can view the array by adding debug($tasks); in the view. You must have debugging turned on to see the array. You will notice that each record is contained in an associative array, where the key matches the model, or “Task”.</p>
<p>We now have a very simple, but functional controller. Our next tutorial will focus on learning about the form helper, getting data into our database from a form, validating that data through the model, and furthering your understanding of models and controllers in general. Until next time!</p>

<!-- Start TwitterButton -->
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><div class="twitter-button"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-designers-part-ii/" data-via="goredmonster" data-text="Introduction to CakePHP for Designers: Part II">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-designers-part-ii/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Introduction to CakePHP for Web Designers</title>
		<link>http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-web-designers/</link>
		<comments>http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-web-designers/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 06:00:59 +0000</pubDate>
		<dc:creator>Paul Redmond</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.goredmonster.com/?p=165</guid>
		<description><![CDATA[
CakePHP hits the sweet-spot for many of my projects. The conventions and “baked” in classes are huge productivity boosters. Even if you just intend of working with visual elements on a CakePHP project (views), this series will walk you through hands-on lessons of building a simple task management application with CakePHP.]]></description>
			<content:encoded><![CDATA[<p>CakePHP for web designers, is a series of hands-on tutorials that help new programers and those with a design background understand the basics of CakePHP. There is plenty of theory and API documentation out there on the web; I’d rather spend my time writing about hands-on examples of how you can start using CakePHP today.</p>
<p>CakePHP hits the sweet-spot for many of my projects. The conventions and “baked” in classes are huge productivity boosters. Even if you just intend of working with visual elements on a CakePHP project (views), this series will walk you through hands-on lessons of building a simple task management application with CakePHP.</p>
<h2>Installing CakePHP</h2>
<p>This series is focused on working with CakePHP, so I don’t want PHP installation interfering with the content of this series. I’ve included installation notes at the bottom of this tutorial to get Apache, PHP, MySQL going. For a full explanation of how to get CakePHP set up properly, see section <a title="CakePHP Requirements" href="http://book.cakephp.org/view/28/Requirements">3.1</a>, <a title="CakePHP Installation Preparation" href="http://book.cakephp.org/view/29/Installation-Preparation">3.2</a>, <a title="CakePHP Installation" href="http://book.cakephp.org/view/32/Installation">3.3</a> and <a title="CakePHP Configuration" href="http://book.cakephp.org/view/39/Configuration">3.4</a> of the <a href="http://book.cakephp.org/">CakePHP book</a>.</p>
<p>In a nutshell, you will download CakePHP’s latest stable version at <a href="http://cakephp.org">http://cakephp.org</a> and upload the files somewhere inside the apache document root on your development machine. I’ve installed a fresh version on http://localhost/cakephp-designers/</p>
<p><img class="alignleft size-medium wp-image-169" title="cakephp_designers_01" src="http://www.goredmonster.com/wp/wp-content/uploads/2009/12/cakephp_designers_01-300x234.png" alt="cakephp_designers_01" width="300" height="234" /></p>
<p>What you see in screenshot #1 is the default CakePHP page with info about your environment configuration. Obviously, we don’t want this to be the homepage of our website, but it’s nice to see something when we get CakePHP installed correctly. This page has useful information about your configuration, and potentially, gives you warnings of steps you missed during setup.</p>
<p>CakePHP applications follow the <a href="http://book.cakephp.org/view/10/Understanding-Model-View-Controller">MVC software design pattern</a> which we will tackle later in this series. Learning the MVC software paradigm is essential to using CakePHP, and the payoff is clean code that is more secure and easier to maintain. This post will be focusing on the “V” in MVC, or the View.</p>
<p>Navigate to your project’s app &gt; views folder. Create a folder called “pages” and inside pages, create a file called “about.ctp”. The file extension “.ctp” is CakePHP view files and accepts HTML and PHP code. Lets add a basic placeholder for our about page in about.ctp:</p>
<pre>&lt;h1&gt;About&lt;/h1&gt;
&lt;p&gt;This is the about page for our application&lt;/p&gt;</pre>
<p>Navigate to your newly create page by navigating to your project url. Mine is http://localhost/cakephp-designers/pages/about Notice that our page is displaying much more than our simple HTML. View the source and you will see the entire HTML structure of your project. The doctype and other HTML code belong to the layout.</p>
<h2>Your Layout</h2>
<p>Right now our project is using the built-in CakePHP layout, but we want to work with our own layout. By default, CakePHP looks for default.ctp in app &gt; views &gt; layouts but if you look in your project you will not find default.ctp anywhere. Create default.ctp and throw in the following HTML:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Tasks&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;?php echo $content_for_layout; ?&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Nothing special here except $content_for_layout. This variable is the output of your view. If you browse to your about page now, you will see the new template in action, and and unstyled site.</p>
<p>Create two more pages, in app &gt; views &gt; pages called index.ctp and contact.ctp and paste in the following:</p>
<pre>&lt;h1&gt;Contact Us&lt;/h1&gt;
&lt;p&gt;Contact info for our application will go here.&lt;/p&gt;</pre>
<p>and</p>
<pre>&lt;h1&gt;Welcome!&lt;/h1&gt;
&lt;p&gt;Welcome to my task management site.&lt;/p&gt;</pre>
<h2>Linking</h2>
<p>Now we have three separate pages, but no way to navigate between each page. CakePHP has a built-in helper called the HTML Helper that makes navigation links within a CakePHP application a breeze; and you can be certain that your links will always work when you follow conventions, no matter where the application resides.</p>
<p>We could easily create static anchor tags and call it a day, but we would be penalized in the future when we update our routes.php file or if we moved the application to another location. With static HTML links, all links would have to be manually updated each time routes.php was modified with a custom URL pattern.</p>
<p>Lets build the navigation using CakePHP conventions. Open default.ctp and add the navigation to this file so it will always appear in any view using default.ctp:</p>
<pre>&lt;div id="navigation"&gt;
	&lt;?php echo $html-&gt;link('Home',
            array('controller' =&gt; 'pages', 'action' =&gt; 'index')); ?&gt; |
	&lt;?php echo $html-&gt;link('About',
            array('controller' =&gt; 'pages', 'action' =&gt; 'about')); ?&gt; |
	&lt;?php echo $html-&gt;link('Contact Us',
            array('controller' =&gt; 'pages', 'action' =&gt; 'contact')); ?&gt;
&lt;/div&gt;</pre>
<p><img class="size-medium wp-image-170 alignright" title="cakephp_designers_02" src="http://www.goredmonster.com/wp/wp-content/uploads/2009/12/cakephp_designers_02-300x166.png" alt="cakephp_designers_02" width="300" height="166" /></p>
<p>If you view your project in a web browser, you will notice the magic that CakePHP performed. All the links were generated with the correct url. Unfortunately, if you click on the home link, it goes to the default CakePHP screen. We can fix that with routes.php.</p>
<h2>Routes</h2>
<p>Routes are an advanced topic, so we will only lightly touch on their power. Routes enable you to do magical things with your URLs and how those URLs relate to your application logic. For now, we only want our index.ctp file to be the homepage our our application. Open routes.php in app &gt; config folder. Around line 34, you should see the default route for the homepage. Lets modify it from this:</p>
<pre>Router::connect('/', array('controller' =&gt; 'pages', 'action' =&gt; 'display', 'home'));</pre>
<p>to this:</p>
<pre>Router::connect('/', array('controller' =&gt; 'pages', 'action' =&gt; 'display', 'index'));</pre>
<p>Save and close routes.php. Make sure you only modify the above code&#8211;this file is critical to CakePHP. The routes.php file now considers index.ctp the homepage of the application, and you will now see your homepage in your web browser.</p>
<h2>Styling Your Application</h2>
<p>To conclude this tutorial, lets add some basic styles to our application. Your website assets are all located in app &gt; webroot in their respective folders. The Webroot folder is the webroot of your application, so http://localhost/cakephp-designers/css in our project points to app &gt; webroot &gt; css.</p>
<p>Browse to app &gt; webroot &gt; css and create a file called style.css. You will also notice cake.generic.css which is the default css file used on the default homepage.</p>
<p>Add the following CSS rules to style.css:</p>
<pre>body {
	background: #ddd;
	font: 12px "Lucida Grande", "Trebuchet Ms", sans-serif;
}
#wrap {
	margin: 20px auto;
	background: #fff;
	width: 800px;
}
#navigation, #footer {
	background: #333;
	padding: 8px 10px;
	color: #fff;
}
#navigation a {
	color: #fff;
	text-decoration: none;
	font-size: 14px;
	margin: 0 15px;
}
#content {
	padding: 5px 20px;
}
#content h2 {
	margin: 0;
}
#footer {
	font-size: 11px;
}</pre>
<p>Update default.ctp to match the following:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Tasks&lt;/title&gt;
	&lt;?php echo $html-&gt;css(array('style.css')); ?&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div id="wrap"&gt;
		&lt;div id="navigation"&gt;
			&lt;?php echo $html-&gt;link('Home',
                            array('controller' =&gt; 'pages',
                            'action' =&gt; 'index')); ?&gt; |
			&lt;?php echo $html-&gt;link('About',
                            array('controller' =&gt; 'pages',
                            'action' =&gt; 'about')); ?&gt; |
			&lt;?php echo $html-&gt;link('Contact Us',
                            array('controller' =&gt; 'pages',
                            'action' =&gt; 'contact')); ?&gt;
		&lt;/div&gt;
		&lt;div id="content"&gt;&lt;?php echo $content_for_layout; ?&gt;&lt;/div&gt;
		&lt;div id="footer"&gt;
                    Copyright © &lt;?php echo date("Y"); ?&gt;. All Rights Reserved.
                &lt;/div&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p><img class="alignleft size-medium wp-image-171" title="cakephp_designers_03" src="http://www.goredmonster.com/wp/wp-content/uploads/2009/12/cakephp_designers_03-300x136.png" alt="cakephp_designers_03" width="300" height="136" /></p>
<p>There are some new things in default.ctp that you might have noticed. In the  of the document, I used another HTML helper method called “css” which generates a link tag to each css file listed in the array. This method automatically links to our css files in the webroot folder. This requires no additional configuration in the future, and is an important convention in CakePHP. I also threw in a date() function to show off vanilla PHP in our templates =).</p>
<p>Now our application is beginning to look alive, but in a boring, static way. Our next tutorial in this series will fix that, as we cover models and controllers. We must cover both simultaneously as models and controllers work closely together to deliver dynamic data in view files.</p>
<h2>Conclusion</h2>
<p>We just tapped the surface of the powerful features of CakePHP views. CakePHP has made it easier to create a consistent workflow for designers and developers. As you will begin to understand, following CakePHP and MVC conventions make life easier for future developers working on your applications.</p>
<p>We’ve covered a vast amount in a short span, but most of the learning will come from experimenting and diving into the code. Please feel free to ask questions in the comments or email me directly.</p>
<p><strong>Notes:</strong><br />CakePHP has a few requirements (http://book.cakephp.org/view/28/Requirements), which will work with pretty much any machine. In a nutshell, you need to configure Apache (enable mod_rewrite), PHP 5, and MySQL to get CakePHP going. CakePHP is flexible and compatible with both PHP 4 and PHP 5. Please use PHP 5. Please? Thank you.</p>
<p>My favorite guide for installing Apache, PHP, and MySQL is contained in <a href="http://foundationphp.com/phpsolutions/">PHP Solutions</a> by David Powers. I recommend purchasing this book if you are just learning how to program PHP. David Powers is a wonderful instructor, and the book does a marvelous job of teaching new programmers PHP. Other more simple solutions include MAMP (mac) and <a href="http://www.microsoft.com/WEB/platform/phponwindows.aspx">PHP on Windows</a> for Windows users. I am assuming Linux users can get LAMP going and don’t need advice <img src='http://www.goredmonster.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Depending on your configuration, you might need to add RewriteBase /path/to/your/cake/files/ to each of your .htaccess files to get CakePHP url rewriting to work properly. If you give up on getting this working, you can open app &gt; config &gt; core.php and uncomment line #57 //Configure::write(&#8216;App.baseUrl&#8217;, env(&#8216;SCRIPT_NAME&#8217;));. You must also delete all .htaccess files to not use mod_rewrite.</p>

<!-- Start TwitterButton -->
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><div class="twitter-button"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-web-designers/" data-via="goredmonster" data-text="Introduction to CakePHP for Web Designers">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.goredmonster.com/cakephp/introduction-to-cakephp-for-web-designers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Shrink your JavaScript/CSS in WordPress with PHP</title>
		<link>http://www.goredmonster.com/php/shrink-your-javascriptcss-in-wordpress-with-php/</link>
		<comments>http://www.goredmonster.com/php/shrink-your-javascriptcss-in-wordpress-with-php/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 08:33:53 +0000</pubDate>
		<dc:creator>Paul Redmond</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.goredmonster.com/wp/?p=30</guid>
		<description><![CDATA[I came across a very simple, yet elegant solution for shrinking JavaScript and CSS files with PHP. When large JavaScript frameworks are the order of the day, why not optimize? In this post I'll show you how to easily apply this script to your Wordpress theme; shrinking your CSS/JavaScript assets by around 300%! ]]></description>
			<content:encoded><![CDATA[<p>I came across a <a href="http://rakaz.nl/2006/12/make-your-pages-load-faster-by-combining-and-compressing-javascript-and-css-files.html">very simple, yet elegant solution</a> for shrinking JavaScript and CSS files with PHP. When large JavaScript frameworks are the order of the day, why not optimize? In this post I&#8217;ll show you how to easily apply this script to your <a href="http://wordpress.org">WordPress</a> theme; shrinking your CSS/JavaScript assets by around 300%! I intend on making a WordPress plugin that automates this process, but for now, this post will show you how to configure your own theme.</p>
<p>This site compresses MooTools to about 20kb (the original file with YUI Compression weighing in at 65kb). Check it out!</p>
<p><img class="alignnone size-full wp-image-71" title="JS File Compressed" src="http://www.goredmonster.com/wp/wp-content/uploads/2009/10/Picture-1.png" alt="JS File Compressed" width="552" height="135" /></p>
<h2>The Setup</h2>
<p>This will literally take minutes to integrate.</p>
<ol>
<li>Download the <a href="http://rakaz.nl/2006/12/make-your-pages-load-faster-by-combining-and-compressing-javascript-and-css-files.html">PHP script</a>.</li>
<li>Upload the file to your active WordPress Theme folder.</li>
<li>Create a folder called &#8220;cache&#8221; &#8211; <strong>Make sure this folder has proper permissions</strong></li>
<li>Create an .htaccess file in your theme folder.</li>
<li>Create a folder called &#8220;css&#8221;</li>
<li>Create a folder called &#8220;javascript&#8221;</li>
</ol>
<h2>Making it Work</h2>
<p>Insert the following code into your .htaccess file in your active theme. Make sure to replace RewriteBase with the absolute path to your theme folder where your combine.php script resides. Note that I made RewriteRule to be a relative path to combine.php:</p>
<pre>RewriteEngine On
RewriteBase /wp/wp-content/themes/yourtheme/
RewriteRule ^css/(.*\.css) combine.php?type=css&amp;files=$1
RewriteRule ^javascript/(.*\.js) combine.php?type=javascript&amp;files=$1</pre>
<p>You&#8217;re ready to link your Javascript and CSS files. As described on the authors site, you link your stylesheets and scripts by chaining with commas. Here is how it will look in your WordPress theme file:</p>
<pre>&lt;link rel="stylesheet" href="&lt;?php bloginfo('template_url'); ?&gt;/css/styles.css,
another.css,athird.css" type="text/css" media="screen" /&gt;

&lt;script src="&lt;?php bloginfo('template_url'); ?&gt;/javascript/jquery.js,
another.js,etc.js" type="text/javascript"&gt;&lt;/script&gt;</pre>
<h2>Notes</h2>
<p>You need to consider script dependencies, and chain your scripts accordingly. Also, make sure your stylesheets are added in the correct order or you may notice some rules being overridden. Depending on your server configuration you might have to chmod your cache folder to 777. Confirm that your cache folder contains your cached files with a &#8216;.gzip&#8217; extension.</p>
<p>All CSS files are moved to the CSS folder in the theme. This goes against the WordPress convention of style.css in the theme&#8217;s main folder. I still have theme comment data for WordPress in my original <a href="http://www.goredmonster.com/wp/wp-content/themes/redmonster/style.css">style.css</a> file, but I don&#8217;t use it on my site.</p>
<p>Major hat-tip to <a href="http://rakaz.nl/">Niels Leenheer</a> for creating such a great script and simple solution.</p>

<!-- Start TwitterButton -->
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><div class="twitter-button"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="http://www.goredmonster.com/php/shrink-your-javascriptcss-in-wordpress-with-php/" data-via="goredmonster" data-text="Shrink your JavaScript/CSS in WordPress with PHP">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.goredmonster.com/php/shrink-your-javascriptcss-in-wordpress-with-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

