Cakephp

Multiple Validation as Behavior in CakePHP 1.2

30 Jul 2008

After some inspiration from a recently published Bakery article, I decided to convert my multiple validation function into a Behavior. Even better, I've thrown it into my plugin collection to make it super easy to drop into any project. Using the new behavior is much like using the script as it was before. You can name the validation properties to include the action name and it'll automatically s...

Read it all »

Creating a Contact Form in CakePHP 1.2

24 Jul 2008

Most contact forms take a name and feedback from a user and mail it off to the administrator but don't store any information in the database. This tutorial will show how to use CakePHP's models, even when no table is being used. CakePHP is great in that you set up a database table, define an empty class file for the Model and suddenly you're halfway there. The Form helper can automatically displa...

Read it all »

Multiple Validation Sets in CakePHP 1.2

23 Jul 2008

In CakePHP, you define how your data should be validated by setting parameters on the validate property of your model. In version 1.2, there is an on option that can be set on a specific rule that, when set, is either create or update. This allows you to define different rules depending on the type of action being performed. That, in combination with the required and allowEmpty properties, give yo...

Read it all »

CakePHP Debug Styles

10 Jul 2008

I just wanted to document this somewhere but whenever I work on a CakePHP project, there's always the debug info that sits at the bottom of the page. It's big and bulky and once I'm off the default CakePHP styles, it's usually pretty ugly. I've done various stylings but I finally took a moment to implement a really simple idea: Fixed position the table and then use hover styles to toggle the vis...

Read it all »

Easier Static Pages for CakePHP 1.2 Update

04 Jul 2008

So it seems that the old code I had didn't work in debug mode, although on the current CakePHP 1.2 RC2, I'm not even sure it works at all. However, I decided to take some time to revisit the code. Last time, I simply hacked up the missingAction and missingController calls and it felt kludgy. It looked kludgy. This time I took a closer look at how the dispatch was being handled. Daniel Hofstetter...

Read it all »

Snogs

09 Jun 2008

If there's ever a quintessential application to build when you first dive into a framework, it's the blog. That's what I did with Snook.ca when I first got into CakePHP. Then for a recent client project, also built using CakePHP, I was asked to integrate a blog. I took this opportunity to take a chunk of the code I used to build my own site and break it out into its own blog plugin: Snogs. Snog...

Read it all »

Easier Static Pages for CakePHP 1.2

04 Feb 2008

This page is out of date. Please read the update. Traditionally in a CakePHP application, to do static pages you have two options: Use the built-in Pages controller and either have all static pages reside in /pages/pagename or set up custom routes. Set up an empty action in a controller. I didn't like either of those options. With Nate Abele's help, I wrote up a custom error class th...

Read it all »

Elemental: Conditional Content with CakePHP

23 Jun 2007

I finally got around to implementing conditional content here on Snook.ca. I've wanted to do this since I moved to the new design just over a month and a half ago. The left sidebar has a number of elements like the projects I've worked, quick links, etc. As some people have noticed, if the article was particularly short, the comments would often overlap the rather lengthy content. I didn't wa...

Read it all »

WithCake.com Revamp

05 Apr 2007

Sometimes you have to try something a few different ways to get it right. When I originally launched WithCake.com, I had used it as a blog to talk about anything I ran into with CakePHP development. However, I quickly found that some posts bordered on the edge of general PHP stuff. Plus, many regular readers of Snook.ca seemed interested in following along even though they might not develop in C...

Read it all »

Delete Cached Models in CakePHP

06 Jan 2007

This seems to bite me in the ass more often than not but any time you add a new model or adjust your associations, be sure to delete the cached ones from the /app/tmp/ folder. I'll get inexplicable errors about missing models and it always takes me a couple minutes to figure it out....

Read it all »

Use Debug CSS in CakePHP

29 Oct 2006

The default layout that comes with CakePHP has some nice styling, especially for the debug table that appears at the bottom. But once you've created your own default layout, you'd have to come up with your own debug styles or copy over the ones from the Cake default. I've done something in between the two. I've created a new stylesheet called debug.css and include it whenever debug mode is on. I ...

Read it all »

Using Uses in AppController

26 Oct 2006

Don't do it. Or maybe there's a better way to do it. The problem I ran into was that I had some Model code that needed to run on every page. So, I thought that I'd add the model to the uses variable in the AppController. The problem with that, as it turns out, is that none of the controllers overrode this property. This meant they couldn't access their models properly. This made leaving a co...

Read it all »

View Caching in CakePHP 1.1.8

22 Oct 2006

The view caching in CakePHP 1.1.8 is both good and bad. It's good in that it can dramatically speed up the performance of your site — when you can use it. And that's the downside. There are certain situations when it simply will not work as a practical option. How it works The caching is all handled through the innards of the framework in a combination of global functions, cache() and cl...

Read it all »

Setting Layout for Users Logged In

16 Oct 2006

If you have anything that you want to run or be available for every controller in your application, you'll need to copy the app_controller.php file from the cake folder to the app folder. User authentication, for example, could be in here (there is an authentication example on the wiki that describes this). One of the common things in an application where you have user authentication is that the l...

Read it all »

Serve HTML instead of XHTML

20 Sep 2006

If there's one thing I don't like about some systems, it's their strict compliance to one standard or the other. Case in point is WordPress that locks you into using XHTML (unless you change it on the fly). I was worried that CakePHP may have done the same but as it turns out, there's a handy file in /cake/config/ called tags.ini.php. Copy that file into /apps/config/ and edit the items in ther...

Read it all »

21 Things I learned about CakePHP

19 Sep 2006

Just came across this and thought I'd pass it along: 21 things I learned about CakePHP. It covers some nice introductory tidbits for CakePHP....

Read it all »

Snook.ca Now Using CakePHP

15 Sep 2006

Things have been pretty busy so I haven't had much time to post about all sorts of cool CakePHP stuff but I did want to take a moment to mention that I relaunched Snook.ca with CakePHP. I had been using Movable Type for about 3 years now but it was limiting my ability to expand on it quickly and easily. Slow rebuilds and server errors pushed me to simply rebuild from scratch. And already I've g...

Read it all »

CakePHP as a Blog Platform

27 Aug 2006

Building a blog using CakePHP is pretty easy. Heck, there's even a basic tutorial to do such on the CakePHP web site. This blog runs off CakePHP and was slammed together pretty quickly. My site over at Snook.ca will one day soon also get switched over to CakePHP. It's really quite flexible and handy for a developer like myself to get something up and running quickly. But can an application be b...

Read it all »

Should Components populate the view?

24 Aug 2006

CakePHP is great with how much flexibility it can give you to approach a given situation. But I'm on the fence about whether components should be able to populate the view (like mini-controllers) or if they should be used only by the controller to pull out required data. Here is two quick examples explaining the difference. The first, I use startup to gain access to the controller and use it to...

Read it all »

Run Your Set Calls as a Batch

18 Aug 2006

This one comes from Nate. Instead of repetitive $this->set calls, you can set all your variables in one go using the PHP compact function. $var1 = 'text'; $var2 = 'text'; $var3 = 'text'; $this->set( compact('var1','var2','var3') ); Thanks, Nate....

Read it all »

Data Always There

17 Aug 2006

Need data to be available at all times and on all pages? Make use of the beforeRender controller callback to include common data. If you need it in all pages in your application, add it to /app/app_controller.php. If you just need it for all pages within a specific controller then you can add it to the callbacks on that specific controller. The beforeRender callback is called after your contro...

Read it all »

Temp Page

15 Aug 2006

I'm sure you've found yourself building a site for a client and you have a temp page for regular users to see and then another URL for your client to go to until the site is ready for launch. In chatting with Yoeri, we thought of just reconfiguring routes. $Route->connect('/', array('controller' => 'pages', 'action' => 'underconstruction')); $Route->connect('/temp', array('controller' =&...

Read it all »

Merging data into $this->data

02 Aug 2006

In CakePHP, $this->data is a good place to store info. It's populated by model information when submitted by the form, it's accessible in the view, and most of the examples show model reads being pushed onto this array. $this->data = $this->Post->read(); I've been changing things up to use array_merge instead. Any new model data will simply overwrite what is currently there and it'll p...

Read it all »

Custom Model Functions

01 Aug 2006

Maybe it's my long time procedural approach to application development but when I jumped into cake, I used much of the functionality to simply replicate my existing development process. For example, the model was used as a database connection class. If it didn't do what I wanted, I'd set up a custom query in the controller, return the results, process, and feed the data to the view. Then I s...

Read it all »

Watch Your Paths

27 Jul 2006

Wow, after spending four hours on a trivial issue, I finally figured out the problem. It was the most bizarre thing. I'd added some logging to see why a session variable was changing from one page to the next only to discover that my action was being executed numerous times per page request (and each subsequent page request changing a time-sensitive session variable). Digging my way through th...

Read it all »

Session Object

25 Jul 2006

Reading and writing values out of the session object is pretty straightforward. But what if you want to store an entire object in there? A common example would be to store user information when logged in. One way to pull out just one property would be to assign the user object back into a variable and grab the fields from there. $userinfo = $this->Session->read('User'); echo $userinfo['id']...

Read it all »

Ease Deployment with Automatic Server Check

16 Jul 2006

Take a peak at the CakePHP manual on configuration and you'll see that you can have two database configurations (and theoretically more, I suppose). Normally you'd have one for your test environment, and one for your live environment. The manual recommends setting the $useDbConfig variable to the configuration of choice in each of your models. Certainly, this isn't very practical when it comes ...

Read it all »

Use Table Aliases in Custom Queries

15 Jul 2006

You've probably noticed that your normal calls to the model return an array that can be referenced through the model name. $data['Post']['id'] When using custom SQL, though, it'll use the table name. Problem is that your table name uses the pluralized and lowercase version of the model name. The easy fix is to define a custom table alias in your query and now your results can be referenced t...

Read it all »

Auto Pluralize

14 Jul 2006

I created a little helper for auto-pluralizing a word using the Inflection class that comes with CakePHP. All I do is pass it the word and the count. The result will be something like, "1 member" or "2 members". class PluralHelper extends Helper { //$s = the string //$c = the count function ize($s, $c) { if($c == 0 || $c > 1) { $inflect = new Inflector(); ...

Read it all »

CakePHP, is it worth it?

14 Jul 2006

After a few weeks of getting into the down and dirty of CakePHP, the question is, "Is it worth using?" The answer to that question is, "it depends". The application does quite well when working with simple models. This is when the application is the funnest to play with. Putting together a blog application, for example, would be really straightforward. What I've discovered, though, is that while ...

Read it all »

CakePHP: Setting Default Values

27 Jun 2006

If you've got a user seeing a form for the first time, normally the fields are blank. But there are times where you want to prefill fields with data or preselect a certain option. Setting a default value turned out to be really straightforward. In my controller, I have a method called create that sets any default values that I might have. class ProjectsController extends AppController { functio...

Read it all »

CakePHP: Data Validation

19 Jun 2006

Validation is one of the basic and most tedious tasks in application development. If there was ever a reason to use a framework this would be it. There are two ways that appear to be advocated by the CakePHP site and while both have benefits, I'm using a slightly different approach. Auto-validation Probably the easiest thing to use is the built-in auto-validation. This is controlled using a $vali...

Read it all »

CakePHP: Bake

14 Jun 2006

In my last post, on my initial thoughts on CakePHP I said: "I'd be incredibly impressed if it had a wizard that let me create the shell for all of this automatically; including a wizard for setting up the database table for the model." As it turns out, I wasn't far off. First, there's a file called bake.php which will, given a parameter or two, generate the controller and model and view files fo...

Read it all »

CakePHP: Initial Thoughts

14 Jun 2006

Despite my previous proclamations, I'm not actually against the idea of using a framework (and in case you're actually not sick of hearing about it, I might have another framework post up my sleeve). But let me set that issue aside. This is really about CakePHP. And I've decided to jump in head first for this project I'm working on. For those of you unfamiliar with it, it's yet another MVC framew...

Read it all »