Posts filed under 'cakephp'

Cakephp Filter gotcha for rails folks

Usually in Rails, if you specify a before_filter in the base controller ‘ApplicationController’ (in application.rb), every other controller in that app inherits that filter, so that even if you specify a before_filter in another controller … the filter in application.rb always runs
Example:

class ApplicationController < ActionController::Base
   before_filter :check_login
end
class UploadsController < ApplicationController
    before_filter :get_data
end

The :check_login method is always run even though UploadsController specifies another before_filter.
(You can stop this behavior by specifying a skip_before_filter :check_login in the Uploads Controller)

However if you take this mindset with you to cakephp you’re in for a frustrating time. Because if you do something like this like this …

class AppController extends Controller{
    function beforeFilter(){
        if($this->Session->read('authenticated') != 'true'):
            $this->redirect('/login');
       endif;
    }
}
class UploadsController extends AppController{
    function beforeFilter(){
        $this->set('file_categories', $this->FileCategory->find('list', array('order' => 'name')));
    }
}

… you’re going to be left wondering why your very hastily thrown together authentication scheme, doesn’t work on the Uploads controller :\

Because the beforeFilter specified in each controller completely *overrides* the one from its parent (in php Object oriented programming, methods in child classes override the matching method in the parent), what you have to do *in PHP* is call the beforeFilter of the parent controller, first, before doing what you want to do.

So instead of the above, you do this …(note the emphasis)

class UploadsController extends AppController{
function beforeFilter(){
   parent::beforeFilter();
        $this->set('file_categories', $this->FileCategory->find('list', array('order' => 'name')));
   }
}

Add comment February 16th, 2009

How to get a hasMany dropdown/select tag in cakephp

Getting a dropdown of items a model hasMany of, should be very easy to do, but I always forget and it takes me far longer to find it than it should.

For this brief example, we’re assuming that we have an Uploads model and a FileCategory model.
Uploads belongs to FileCategory and File Category has many Uploads … get it?

class Upload extends AppModel {
   var $belongsTo = array('Client', 'FileCategory');
}
 
class FileCategory extends AppModel {
   var $hasMany = array('Upload');
}

All you need to do to is get the items for the dropdown with a find(’list’) command and then include it with a FormHelper method parameter.

If most of the actions in your controller will need this dropdown, then you might want to move it into a beforeFilter() like this

function beforeFilter(){
	$this-&gt;set('file_categories', $this-&gt;FileCategory-&gt;find('list', array('order' =&gt;; 'name')));
}

Now, in the view where you want the dropdown to appear, you just call the FormHelper input method like this.

input('file_category_id', array('options' =&gt; $file_categories, 'label' =&gt;; 'File Category: ', 'class' =&gt; 'short')) ?&gt;

and there you have it.

Hope this helps someone.

Add comment February 14th, 2009

Cakephp gotcha: Don’t name controller the same as folder in webroot

This one had me going for a bit, but if you name a controller the same as a file that is in the “webroot” folder, and try to navigate to it, cake will just show you a listing of all the files in the same named directory.

As an example, the “files” folder comes with the cakephp installation right?

Not knowing that, I tried to build a files controller and navigate to it.

but it takes me instead to

 
and here’s the listing of files

I’m thinking that this could actually be used against you as a vulnerability, so keep that in mind too.

Add comment February 13th, 2009


Recommended

Posts by Category

Calendar

March 2010
S M T W T F S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Posts by Month