I have just spent too long trying to discover why a controller throws a “Cannot modify header information - headers already sent by…” error message. There’s no obvious reason why, and no obvious fix. The controller works fine on another installation of Apache2 and PHP, but not on my machine at home.
The problem? White space after the closing ?> PHP tag. You can have as many carriage returns as you like, but don’t try a space. Turns out it was reported here 9 months ago. The team’s response is perfectly acceptable, but it’s quite a hard little caveat to track down and fix! Here’s hoping this will be of use to someone else in distress.
Tags: cake, CakePHP
An excellent and very readable piece on validating file uploads from within a CakePHP model. Provides the basic outline for how to handle the upload, as well as the more technical nitty gritty from within the model.
Tags: CakePHP
There are several good user authentication/registration/login systems available for CakePHP (especially version 1.2), but I decided to roll my own. This offers a good learning exercise, in addition to the benefit of knowing where all your code is and what it’s doing.
The latest little addition I’ve made to the code allows you to redirect the user back to where they wanted to go before they logged in. For example, I want to add a recipe into my online multi-user cookbook web application. If I click an “Add Recipe” button and get taken to a login form, I don’t want to then be redirected to the front page once I’m logged in - I want to go straight back to adding a recipe.
The basic logic of the login system is:
- A function in /app/app_controller.php to check if the user is logged in. If they’re not logged in, redirect them to the login page.
- A login form to talk to a controller and model which do all the processing/checking to see if the details are correct.
- A line of code at the end of all this to redirect the user to the front page (or members area).
By adding a tiny snippet of code to the check-if-user-is-logged-in function, and revising the redirect after login, the user will get a much more pleasant experience:
function checkIfLoggedIn() {
if(!$this -> Session -> check('user')) {
$this -> Session -> write('lastPageVisited', $this -> params['url']['url']);
$this -> redirect('/users/login', null, true);
}
}
Then when, in your controller, you decide that the user is all logged in (in this example you’d set the user variable in the session), use the following snippet:
$this -> redirect('/' . $this -> Session -> read('lastPageVisited'), null, true);
Pretty rough and ready (forgive the prepending slash), but it works for my needs. If you do this, you’ll probably want to check the lastPageVisited variable to ensure it exists.
Do you use CakePHP and go about this a different way? Would love to hear different methods…
Tags: cake, CakePHP, php
This is an updated version of “CakePHP Livesearch” - published here in 2006. That version was designed for CakePHP 1.1, whilst this is a more polished and modern solution for CakePHP 1.2. The two tutorials are not compatible with each other due to changes in the framework. It is recommended that you stick with this one, and use CakepHP 1.2.
A while ago I wrote a tutorial covering how to get livesearch functionality working in CakePHP. A lot has happened since then - new versions of the framework have been issued, my knowledge of all things Cake, PHP & Ajax has deepened. Here’s hoping that my ability to communicate has, also.
There was a lot of positive feedback from the last tutorial which has spurred on a newer, revised tutorial. The contents of this post relate to Cake 1.2 (still in beta, but very usable). Whilst much of the steps are the same, there are a few subtle differences, and I have tried to clean things up a little better. So, without further ado…
Read the rest of this entry »
Tags: cake, CakePHP, php
CakePHP is a PHP framework which is turning out to be most useful, and very flexible. I’ve embarked on a few little projects with it, and thought I would spread a little understanding with regards to the session component - as I struggled to find a concise set of examples to help me on my way.
Read the rest of this entry »
Tags: cake, CakePHP, php