Archive for October, 2007

Modifying Dreamweaver for cakePHP

October 21st, 2007 by ScottK | No Comments | Filed in News

Ok I can hear you now because I still hear it all the time, “Why is he still using Dreamweaver for his coding when xx is so much better”. Quite simply the best thing about it is that I don’t have to think about the ftp structure when putting/getting files. Tell me a program that has code completion, validation and a smart put/get ftp and I’ll listen.

So now that I’ve embraced cakePHP I’ve made a few modifications to my Dreamweaver configuration files so I can work with the thtml and tcp files required by cakePHP as well as adding in the default helpers $html and $form into code completion. I want to share with you as well. So here’s the link to download the instruction files. Dreamweaver cakePHP Modifications

You may also find the download by going through the Widgets & Plugins page.

Part 1: Settting Up the Initial Application

October 21st, 2007 by ScottK | 1 Comment | Filed in cakePHP

CakePHP is really easy to set-up but until you do it once you really don’t know this. The documentation and tutorial at the cakePHP site is really vagaue for how you do things outside of the simple Post Controller/Model example they have. I mean how do you configure the routes file for more than one controller, etc?

This then is Part 1 of a serires on how to use cakePHP and I’ll be discussing on how to configure a new set-up to the point of being ready to develop it into a working site. I’m leaving it to you to grab the version that you want to use from cakePHP and while your at it go ahead and grab the scriptaculous libraries as we’ll be setting those up as well. Make sure to leave a donation if you can to these fine people, hmm thinking about it maybe myself if you’ve enjoyed this help.

So with your files extracted let’s look at the structure and how to upload them to your server. We will be working within the app folder, whether dealing with the routes, database, or adding the scriptaculous library. Specifically take notice of the webroot folder, that is where your “public” files are going to go. That means javascript, css, images etc. So go ahead and extract the scriptaculous library and move all the javascript files into app/webroot/js. Don’t include the lib/ or src/ folders in scriptaculous just the files. It’s not a big deal if you choose to.

Locate the database.php.default file in the app/config/ directory and open it up for modification. Then save that as database.php and use that for the correct configurations. Here is where we are going to set the configuration for database connection. At this point you’ll need to have a database set-up or two if you can. For kicks let’s say I have a database called part_1 and that will be used in this example.

Assuming that MySql database is going to be used here is what my database.php is going to look like:
var $default = array(
  ’driver’ => ‘mysql’,
  ’persistent’ => false,
  ’host’ => ‘localhost’,
  #’port’ => ‘YOUR_PORT’
  ’login’ => ‘YOUR_DATABASE_USERNAME’,
  ’password’ => ‘YOUR_DATABASE_PASSWORD’,
  ’database’ => ‘part_1′,
  ’prefix’ => ”
 );

 var $test = array(
  ’driver’ => ‘mysql’,
  ’persistent’ => false,
  ’host’ => ‘localhost’,
  ‘login’ => ‘YOUR_DATABASE_USERNAME’,
  #’port’ => ‘YOUR_PORT’
  ’password’ => ‘YOUR_DATABASE_PASSWORD’,
  ’database’ => ‘part_1_test’,
  ’prefix’ => ”
 );

You see that I have two databases really part_1, and part_1_test. However using a single database I could just as easily set the prefix in the $test to ‘prefix’ => ‘test_’. I however do not like setting up test tables within actual data tables within a single database so it’s nice that cakePHP can split them out. 

You’ll notice that I also included the ‘port’ => and have it commented out but you’re not seeing it in your database.php file. I included it to show that you can have a set-up for a not default MySQL configuration and this is how you do it. I’ve run databases that listen to ports 3307 or the socket files are in custom places so cakePHP would not be able to get connected without the port being set. Uncommenting the port and pointing in the right direction worked fine.

The next step is to set-up the config file located at app/config/core.php. Here we will be making some changes to the default for use in our application and to alert you to some functional changes of cakePHP.

The first item of interest is around line 85 or search for CAKE_SESSION_SAVE. By default cakePHP saves the sessions as defaulted in the servers php.ini. Generally this means saving it as a file somewhere on the server and for shared hosting this is bad. In general saving sessions to a file anywhere other users can access them is a bad idea. Even though one of the settings “cake” means saving the fiels to the app/tmp directory it’s still best to store the session information in the database. So change the CAKE_SESSION_SAVE to database.

With that done you’ll need to change the CAKE_SESSION_TABLE to the table name in the database you want to use. I leave mine at the default cake_sessions. However now you need to create the sessions table in the database. In the app/config/sql/sessions.sql file you’ll find the sql to create the table. So however you choose run that now.

Find CAKE_SESSION_STRING and change that to something other than what is printed. This string is used to create session id’s so if you leave it as is not only will cakePHP complain but you potentially have the same session id’s as every other cakePHP user. Changing as many characters will make your site more unique session wise.

I also like to set the CAKE_SESSION_COOKIE to something more descriptive of my application. So here I’ll be changing it to part_1_tutorial. Your choice really.

Unless you really don’t need a site administration area then don’t find CAKE_ADMIN and uncomment it. Otherwise do find it and uncomment it. A final note in the core.php file is the WEBSERVICES setting. As of cakePHP 1.2 this has been depreciated and cakePHP complains if you set to true. leave it as false.

Now you may upload the app/, cake/, docs, vendor/, .htaccess, index.php folders and files from the cakexxxx folder to your hosts web root folder, (generally public_html). Go ahead I’ll wait.

With all the files uploaded you should be able to point your browser to yourdomain.com and see the intro page. Hopefully the only problem you see is that the tmp/ folder may not have write permissions. To fix this you need to set the permissions on app/tmp/ folder to allow apache to write files in there. This is for the caching of cake pages. Once you get this done you should see that the databse is connected, tmp folder is writable and even session queries at the bottom of the page.

To get rid of the default cakePHP page simply create a page in app/views/pages/ called home.ctp. This is the default page if no controller is called in the uri.

So there you have it a quick and easy set-up of a cakePHP app. It probably took you longer to read this and upload the files than actually making it work and that is one benefit of cakePHP. I intentionally did not discuss routing as that is more of a topic on controller set-up.

I Made a Cake With PHP Goodness

October 20th, 2007 by ScottK | No Comments | Filed in Programming

I like to think of myself as a horizontal programmer that looks at different languages rather than a vertical programmer which specializes in one language. In terms of the Model-View-Controller architecture I’ve programmed in C# and now Ruby on Rails. I knew however that there was other languages that could support this architecture and I wanted to try them out.

As far as Ruby on Rails goes there is a ninche market for web hosting for rails apps. Because of that I don’t feel right about being limited to my hosting choices if I ever get upset with the provider. I did try to set up my home box with mongrels and completely messed it up. So to me it’s the hosting requirements that make a Rails app un-popular to me.

I’ve also looked at Django and that is a sweet framework. I’ve recently taken a likeing to Python and Django makes use of it to deliver the framework. One of the excellent points is that an administration page can automagically be created as well so application development is even faster. Since one Django project can hold many applications the admin section has access to all of these. I run many sites that have admin sections and this would be a great feature.

However Django being written in Python requires the mod_python module in Apache and an entry in the virtual host file. On my home box that is not a problem. On a hosting provider though it may be a problem. In part they provider may not have mod_python installed nor would they want to alter the conf file.

Another issue I found with Django is that it is still young and going through active development. The current stable version being .96 works but the development version .97 has made some core changes so depreciation warning are going to happen. Also clearly stated serialization is under heavy development and changes are being made to the authorization modules. So as sexy as it is I think I’ll wait a few versions.

I just spent all of today playing with CakePHP. CakePHP is a MVC framework written in PHP so naturally I must admit I am biased when I say I’m going to love it. Along with the framework you are given tools to generate new models, views, and controllers from the command line and choice are included for scaffolding and tests as well. That’s known as cake bake. :)

Before I explain my experience let me say I am using the development version 1.2.0x alpha version and not the 1.1.17.x stable version. Although I did have to down grade to test something and discovered that there are differences between the two in the behind the scenes code. Plus several key functions are going to become depreciated in favor of better organization. I’m sticking with the development version and if you’re wanting to try cakePHP then go with that also.

For starters the documentation leaves a lot to be desired. The API manuals are just fine so that’s a bonus but it’s the other documentation such as setting up the routes that hurt. The tutorial does explain this with a Post example but it would have really been helpful to see a routes file set up for a Post controller and a News controller. Now granted it only took me two hours to figure out and fix the problem on my end as to why mod_rewrite was not working. Once I got that done the routing took over nicely.

After a few learning experiences I finally got a default layout up and running with a few test different routes for testing. Everything went well and as expected until I decided to start including javascript. Even though it’s offered as a helper the JavascriptHelper is not available to views without being included in a controller. HtmlHelper and FormHelper are available but those do not allow me to do the script include in the head section. That’s bad in my opinion. Nor is there a generic helper application file. Something I could just pop in a quick helper and it’s globally available, something like the script includes I just mentioned.

It may seem that I am displeased with cakePHP and that is not the case. I am totally looking forward to working with it. Once of my concerns was the file structure. Although the tutorial suggests to point the host directory in the apache config file to the cake webroot you do not need to do this. just upload the whole kit and kaboodle to the web root folder and it will work. That means any hosting provider that supports PHP and allows you to modify your htaccess will work with cakePHP. In less than twleve hours I took cakePHP with no knowledge and had a working application. Even made my first helper for the head meta tag stuff.

Whether it is or isn’t Ruby on Rails is just to difficult to manage and hosting options are limited. Django is just a bit to young at the moment but when it matures it’s an awesome framework. cakePHP is both stable and active with a huge base of potential contributors from the PHP community. For now call me a baker of cake.