Archive Page 2
CakePHP ACL Behavior modified
I’ve posted this up at the bakery, but articles seem to take awhile to get published. It could also be that they’re working on a new ACL Behavior, one can hope. For the time being, I’ve fixed up the core behavior and added a few features as well. I’ll just replicate here what I wrote at the bakery.
This Acl behavior makes a number of improvements from the built-in one. It allows you to have a model act as both an “Aro” and “Aco”. It creates an alias in this format “Model.id”. If parent node is not provided, it’ll create one based on root object. For example, if you create Post.1 as an ACO and you set parentNode to null, it’ll set the parent_id to the root “Post” Aco if it exists. So your tree would look like the following:
Acos ---------- Post |-Post.1 |-Post.2 |-Post.3
Usage Instructions
Follow the following steps to use the Acl behavior.
Step 1
Copy the behavior class into a file named “acl.php” in your /app/models/behaviors folder.
Step 2
Load the behavior in the model you want to use it in.
Examples:
Ex 1: Act as Aco
var $actsAs = array('Acl' => array('Aco'));
Ex 2: Act as Aco
var $actsAs = array('Acl' => 'Aco');
Ex 3: Act as Aro
var $actsAs = array('Acl' => array('Aro'));
Ex 4: Act as Aro and Aco
var $actsAs = array('Acl' => array('Aro', 'Aco'));
Step 3
Create the method parentNode($type) in your model. This will return the parent_id to a method in the Acl behavior.
Here’s an example of a User model passing a group id as it’s parent id only for the ARO:
function parentNode($type)
{
if ($type == 'Aro') {
if (!$this->id) {
return null;
}
$data = $this->read();
if (!$data['User']['group_id']){
return null;
} else {
return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
}
} else {
return false;
}
}
That’s it!
Download the behavior class here
Filed under: CakePHP, Development | Leave a Comment
Cheat sheets
Sometimes it’s hard to remember certain things about technologies you use when developing a web application. I must admit that I frequent php.net occasionally to remind myself what parameters a function takes. With frameworks, extra php libraries, javascript libraries, and css it becomes increasingly more difficult to remember how to use all of these technologies. In comes the cheat sheet. Cheat sheets are great as a quick reference on how to use common features with a particular technology.
Here is a short list of useful cheat sheets:
Prototype 1.5 – by Jonathan Snook
Mootools r.83 – by Jonathan Snook
Scriptaculous – by Ryan Carter
CSS by Dave Child
You can find more of cheat sheets (PHP, mySQL, Javascript, etc) over at Dave Childs cheat sheet section
Filed under: General | Leave a Comment
PHP developer vs scripter
Jim Plush wrote an interesting article on what differentiates a PHP developer from a scripter in his opinion. He specifies in the intro that the criteria is for an ideal senior level developer. I agree with most of his criteria aside for the CSS requirement. I think it’s good for a developer to know CSS but if they are at the senior level then there’s a good chance they would never have to deal with the CSS in a project.
As for the other criteria he mentioned… I really need to touch up on unit testing :\
http://www.litfuel.net/plush/?postid=166
Filed under: General | Leave a Comment
PHP 5 Zend Certification blog
I came across a nice little blog that can help you pass the PHP 5 certification exam. It will be covering the following sections:
1.- Syntax
2.- Anatomy of a PHP Script
3.- Data Types
4.- Variables
5.- Consts
6.- Operators
7.- Control Structures
8.- Errors and Error Management
Not bad for starters. The official study guide may not be enough so it’s always good to look around for other resources.
Filed under: General | 1 Comment
Image resize helper for Cake
A nice little helper for resizing images was published today by Josh Hundley at the bakery. The helper caches the resized image as well so that resize functions aren’t called every time the script is called.
When including this helper it is fairly easy to resize the image in your views.
Just use the following code:
echo $image->resize('myimage.jpg', 150, 150, true);
You can download the helper here
Filed under: General | Leave a Comment
PHP the Smarty Way
What is Smarty? It’s a php templating engine. I’ve used Smarty on several projects and I like it’s simplicity. It forced me to keep my application logic separated from the presentation code. This is a must when organizing your code. The last thing you want is your designer browsing through hundreds of lines of code just to apply some html. Furthermore, Smarty comes with extra features such as built-in caching, vairable modifiers, filters, and much more including user contributed plugins.
How to install Smarty
Installing Smarty is a fairly simple process. I will break the installation process into 3 steps.
STEP 1 – Download and Extract
Grab the latest Smarty release from Smarty’s download section then extract the package into your application directory. Now rename the new directory created by the archive from “Smarty-x.x.x” to “smarty” for the sake of simplicity.
STEP 2 – Create required folders
Smarty requires some folders for caching purposes among other things. In your smarty directory create the following folders:
/path/to/application/smarty/cache
/path/to/application/smarty/configs
/path/to/application/smarty/templates
/path/to/application/smarty/templates_c
Then change the permissions of the newly created cache and templates_c folders to 755
STEP 3 – Create the smarty setup file
Create a file called smarty.php and put this wherever your application configuration folder is located. You’ll want to include this file in your application php files. So if you use a global index.php file or configuration file include it in there.
Here’s the following setup code for smarty.php
// put full path to Smarty.class.php
require('/path/to/application/smarty/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = '/path/to/application/smarty/templates';
$smarty->compile_dir = '//path/to/application/smarty/templates_c';
$smarty->cache_dir = '/path/to/application/smarty/cache';
$smarty->config_dir = '/path/to/application/smarty/configs';
That’s it. Now whenever you want to display a template file, you’ll need to create that file in your templates folder (ie: /path/to/application/smarty/templates/index.tpl) and then you would call this template from your php file with the following code
$smarty->assign('title', 'Hello World'); // creates a variable 'title' with the string "Hello World" in your template file
$smarty->display('index.tpl');
In your template file you can access the title variable using the following syntax: {$title}
This is a fairly simple way to get started with Smarty.
Here is a crash course on using this engine from the Smarty website – Smarty Crash Course
If you’re already familiar with Smarty, here’s a handy cheat sheet from somewherein.
One last thing. If you’re a CakePHP user I don’t really recommend using Smarty because CakePHP’s template system is much more convenient, nonetheless, there is a way integrate Smarty with CakePHP. Read the article at the bakery.
Filed under: Development, General | Leave a Comment
Windows Apache MySQL PHP (WAMP)
There’s a few WAMP packages out there that make having a php web server on windows as simple as installing a program. I started off using Xampp from apachefriends.org but I had problems running cakePHP with it. Both Xampp and CakePHP have made many improvements since then and they are now compatible with each other. However, when I had problems I switched to WAMP5 from wampserver.com.
In terms of features, both packages are similar. However, Xampp offers SSL support and WAMP5 does not. WAMP5, on the other hand, is lightweight and about half the size of the Xampp package. You can also add SSL support for WAMP5 by following this tutorial.
Wiki has a nice article comparing all of the main WAMP packages out there. Read more
Filed under: Development, General | 1 Comment
Joomla meets CakePHP
Since I’ve posted the Drupal article, I was hoping that Joomla would be the next CMS to have integration with Cake. What I like about Joomla is their administration interface. I’ve been playing around with Joomla 1.5 Beta 2 and they have really cleaned it up nicely. Unfortunately it’s taking them awhile to release a stable version, but I’m sure we’ll be pleased once it does finally released.
After doing some searching it looks like the Joomla – CakePHP integration has become a reality. The folks at Giga Promoters have posted an article on how this can be done. Read the full story here
Filed under: Development, General | 2 Comments
PHP 5 Certification Exam
I’ve recently been studying for the Zend PHP 5 certification exam and so far the study guide has been a disappointment. I’ll soon write a full review for the study guide, but at the moment I’m looking to helpful resources for passing the exam. I’ve read it is much harder than the PHP 4 exam, which doesn’t surprise me, but it’s a bit discouraging reading posts in the PHP | Architect forums that the study guide and practice tests didn’t help. I keep passing the practice tests, but from what I hear they are easier than the real thing, contrary to what php architect says.
If anyone one has any helpful resources I’d appreciate it if you emailed me or posted on here.
Filed under: Development, General | Leave a Comment
Drupal 5.0 released
Drupal is a powerful open source content management system and on Monday, January 18th, they have released version 5.0. I strongly recommend this CMS as it’s one of the most flexible systems in the open source market. Also, it’s free!.
To see this CMS in action, click here to view a demo
Or you can install it yourself by downloading it from Drupal.
For the CakePHP users out there, you can create third party modules using cake for Drupal. Read this tutorial if you’re interested
Filed under: Development, General | Leave a Comment
Search
-
You are currently browsing the Steve's Journal weblog archives.