While working on the migration of a Cake1.x project to Cake2, I faced a problem with the JsonView plugin of Pagebakers. Instead of being catched by the JsonView View, it just printed out the plain old html error of a missing view.
So what changed?
The variable “$this->view” which was used in Cake1 was replaced by $this->viewClass in Cake2. This was the number one cause of the problems.
Other cause, was the plugin not being loaded using the CakePlugin system:
CakePlugin::load('JsonView');
NOTE - Don’t forget to set the response type too!
// set content-type to application/json
$this->response>type('json', 'application/json');
Today I’m migrating one of our webapplications from Cakephp 1.3 to Cakephp 2.0.5.
The upgrade itself was performed by calling the shell command script:
cake upgrade all
But one thing I’ve been unable to find googling around was the answer to the error
Controller could not be found
Luckily I printed out the migration guide from http://book.cakephp.org , where this section of Routing changes pointed out that I’d had to add the following statement:
require CAKE . 'Config' . DS . 'routes.php';
This solves the problem of “Controller could not be found”
A lot of webdevelopers do repeating tasks on each project they make. One of these repeating tasks is building your CSS too make the project look great! With bootstrap - which is a project of twitter - most of all CSS styling is already done for you and enables you to easily extend it.
You can find Bootstrap at :
http://twitter.github.com/bootstrap/
Recently I tried to integrate the php spellchecker of TinyMCE into a project we’re working on, when I stumbled upon the error:
uncaught typeerror cannot call method ‘push’
Googling around I found the solution to the problem by upgrading the TinyMCE package to a newer version (3.4.5). Hope it helps - because I didn’t found that much information on the internet about it.
If you trim files that we’re exported using UTF8 encoding then be carefull that you also trim the non-breaking spaces in it. The default options of trim() do not include this, so you have to use the following code to be sure to also trim them:
// turn non breaking space into 'normal' string // to behave exactly like a non-breaking space $myHTML = " abc";
$nbSpace = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES));
$converted = strtr($myHTML, $nbSpace);
// trim utf8 non breaking spaces
$converted = trim($converted,chr(0xC2).chr(0xA0));
Did you knew that you could iterate through an objects variables? Well I did, but I already forgot that it was possible.
Take a look at the following class:
<?php
class Car {
public $tires = 4;
public $doors = 5;
public $color = "black";
public $manufacturer = "Ferrari";
protected $secret = "This is something you may not read";
}
If you use the class mentioned above in some other file, you can iterate through its variables like:
<?php
require_once 'car.class.php';
$car = new Car();
foreach($car as $car_detail => $value) {
print $car_detail." => ".$value." <br>";
}
This results in the output of:
<?php
tires => 4 doors => 5 color => black manufacturer => Ferrari
If you paid really good attention to the class, you might notice that the secret variable is NOT included in our output. This is completely normal like, you cannot access the variable directly when it’s a protected variable.
In this post I’ll try to explain the implementation of MVC (Model-View-Controller) in the Zend Framework. If you are not familiar with Zend Framework, you might read the Introduction to Zend Framework post
I assume you already know the idea behind the Model-View-Controller idea. If not please read the section below - or the MVC section on this page
In short Model-View-Controller comes down to this:
Frontcontroller
Zend Framework is not only using the MVC design pattern, but it’s also using the Frontcontroller design pattern.
The Frontcontroller design pattern requires that all requests are processed through one file - which is a very common practice in a lot of open source webapplications (like Drupal, Typo3, etc.). In most webapplications this is done by using the mod_rewrite module (or similar) to rewrite everything to a file called index.php. One big advantage of this design pattern is that for example, Authorization can be easily implemented into the system.
In Zend Framework this index.php takes care that the Frontcontroller is being instantiated.
Using only one controller (the Frontcontroller) would result in large unmaintainable mess with too many methods.
That’s why we use multiple controllers in Zend Framework to split up the different parts of the application, each in its own controller. For example if you want to build a blog, you might build a BlogPostController where all functionality related to blogposts resides. Every task like creating, deleting or modifying a blogpost is called an action in Zend Framework.
To return to our example of a BlogPostController, that controller will look like this:
[php]
<?php
class BlogPostController extends Zend_Controller_Action {
// method that takes care of creating a blogpost
public function createAction() { }
// method that takes care of modifying a blogpost
public function editAction() { }
// method that takes care of deleting a blogpost
public function deleteAction() { }
// method that takes care of listing blogposts
public function listAction() { }
}
[/php]
In Zend Framework each action has its own URL. Through the naming convention of controllerclasses and actions, Zend Framework is capable of translating an URL to the corresponding controllerclass and action. The translation process from URL to Controller/Action is called Routing. The process that calls the specific controller and action, based on the translated request by the router, is called dispatching.
The request-object ( translated request from the router ) is being passed to the controller from the dispatcher. This request-object is being instantiated by the Frontcontroller, being passed to the router class to be filled with request information and at last being passed from the dispatcher to the corresponding controller.
Below you will find an image that shows the total ZF MVC process.
I will write a dedicated post about views in Zend Framework, you will find this later on.