Monthly Archives: October 2011

File Upload in CakePHP

In this post I will describe how to upload images in CakePHP. It will automatically upload and move the uploaded file to the wanted location. It will automatically rename the file if there is another file with same name exist. A lot of other features are also available.

  • Download Upload Component file and copy it to the location app/Controller/Component/UploadComponent.php
  • Initialize
  • Your add action would looks like:
  • We have setup our controller. Now its time to setup view file add.ctp. The file file should look like following:
  • All done.

UploadComponent class file is well document so you can always find all configuration setting for the uploaded images.

Setting Auth & ACL in CakePHP

In last few days I learned CakePHP and now I am finding it very interesting. I learned the basics about creation of models and their relationships. Now its time to move ahead. I am exploring more features and I started with inbuilt Auth component of CakePHP which I found very interesting. Let me tell you one thing that if you are following the conventions you can save a lot of time.

To set up Auth and ACL for my application I followed Mark Story’s post which is very helpful. I followed the tutorial and I was easily able to setup Auth & ACL for my application.

But in my application every user belongs to some group and it must inherit permission from that group only. There should not be any user wise permission. This case is not handled in the above tutorial. So, whenever I add new user a new ARO record is being created in aros table, which is not required. I dont want any ARO record for my users as they are inheriting permission from their group. To achieve this functionality I modified $actAs variable as follows:

Look at the declaration carefully as I am passing two parameters type and enabled. Second parameter enabled tells the behaviour class not to look for ARO record for User model if there is already a ARO record of parent Group model. Now we have to define a function bindNode to tell that this user is bind to which ARO node. This function must return a reference to group model. So our bindNode function would look like:

Lets Bake in PHP using CakePHP

From last week I decided to learn CakePHP, which is a open source web application framework for producing web applications. It is written in PHP, modeled after the concepts of Ruby on Rails, and distributed under the MIT License.

I started with the official documentation. But I am not finding it very useful when you really get stuck somewhere. But CakePHP has active developer team and very active community.

Here’s a quick list of features you’ll enjoy when using CakePHP:

  • Integrated CRUD for database interaction
  • Application scaffolding
  • Code generation
  • MVC architecture
  • Request dispatcher with clean, custom URLs and routes
  • Built-in validation
  • Fast and flexible templating (PHP syntax, with helpers)
  • View Helpers for AJAX, JavaScript, HTML Forms and more
  • Email, Cookie, Security, Session, and Request Handling Components
  • Flexible ACL
  • Data Sanitization
  • Flexible Caching
  • Works from any web site directory, with little to no Apache configuration involved

Thats all what I get from the initial chapters of CakePHP.