So I’ve been working with CakePHP lately and wonder if there’s any way to get one of my favorite PHP IDE, Zend Studio, to work with it. I was particularly interested in the “automatic code completion” functionality that came with Zend Studio, and thought there should be some way to get CakePHP’s library into that list.
Take $this->flash() for an example. Here is what I can do (shown in the image below) with auto completion from Zend Studio. Instead of having to remember the correct sequence of arguments to fill in the flash() function, I can now read through the text shown and fill them in accordingly. It saves you time, cut down passing wrong arguments to functions and thus errors, and free you from running another browser tab/appliction to cross check the function that you had just used.
*Yeah, I know, I know, flash() is easy to remember but there are some which aren’t as easy. Well, you get my point.
How To Get CakePHP Auto Complete in Zend Studio?
Fortunately, Zend studio can actually add new packages to its code completion library.
From Zend Studio Help File:
Adding New Packages to the Code Completion Library
You can add Classpaths, JARs and folders containing Java files to the project. Doing this enables the methods, classes, functions, etc., that are present in the added packages to be available to the current code completion library.1. Go to Project | Project Properties. The Project Properties dialog will open
2. Select the Java Bridge tab.
3. Click Add Jars or Add Class Folders. The appropriate Add dialog will appear.
4. Locate the JAR / Class folder. Click Select and OK to add the JAR(s) and return to the IDE. The added objects now appear in the code completion library.
The help file assumed that you had a project already defined. If you haven’t do so:
1. Define a new project and have whatever your http://localhost/cakeFolderHere is in your local directory as the path entries. In my case, it’s C:\xampllite\htdocs\cake12
2. Select the default options and press next till you reach the last step where you can add your class folders. Here is where the real magic happens. If you read the documentation carefully enough, CakePHP store all its libs in the folder cake. So in this case, we can tell Zend Studio to look into CakePHP’s libraries (=cake) so that it can grab all classes, functions and definitions to be used in code completion.
Imperfect…
So far I’ve tested, if you are working in a controller, you can only get functions for a Controller (flash(), set(), beforeFilter() etc) but you can’t get to the model’s functions. Meaning you won’t get any code completion when you type $this->User-> in users_controller.php.
You won’t get the helpers in the view either 🙁 Meaning no code completion when you type $form-> in users/view.ctp (v1.2) or users/view.thtml (v1.1)
But code completion in the model looks fine so far.
So there you have it 🙂
I’m pretty sure other PHP IDE (PHPEdit, Eclipse etc) can do the same, so if you found one, please leave a comment 🙂
Richard@Home says
Code completion for models in a controller is pretty much impossible with CakePHP (and any framework that dynamically loads classes) I’m afraid: The Models aren’t loaded until run time so the IDE has no idea they are used 🙁
David says
Richard, I’m afraid I’ve reach to the same conclusion. But it’s better then nothing 🙂
Tuan says
There is code completion with Komodo Edit.. just google komodo and cakephp.
David says
since we’re on this, found out recently that PHPEd 5.2 has integrated code completion for cakephp as well. Their demos and tutorials looks very promising and they have trial versions too.