Archive for the ‘PHP’ Category

Zend Framework Routes and Navigation

Wednesday, March 10th, 2010

Having spent a few hours attempting to work out why Zend_Navigation was refusing to place the built-in “active” css class onto my navigation elements while being in the correct module/controller/action I finally resolved the issue:

It came down the custom routes!

If you have custom routes and you find your navigation is not correctly highlighting  it’s probably because you haven’t tied in your route to your navigation elements.

It requires very explicit setting to work.

Below is an example of how it should be structured:

 (more...)

AMFPHP Cake constructor arguments causing NetConnection.Call.BadVersion

Saturday, October 10th, 2009

So… you’re seeing “NetConnection.Call.BadVersion” from Flash while attempting to talk to CPAMF or AMFPHP.
Well my friend, there’s a good probability that something is wrong.
Here are some suggestions:

  1. Fatal Error
  2. Redirect

What I tracked my problem down to was the fact that the following happens when AMFPHP receives a Flash remote object (using remoting):

  1. AMFPHP receives an AMF stream
  2. AMFPHP inteprets the stream byte by byte
  3. AMFPHP decides what sort of “thing” that Flash has attempted to send back
  4. AMFPHP attempts to “find” the relevant PHP class, defined by the class alias that has been sent in Flash.
    e.g. Flash object is "com.org.my.namespace.path.MyClass"
    and interprets that as com/org/my/namespace/path/MyClass.php (or MyClass.class.php) based on the base classMappingPath you sent in the service gateway, $gateway->setClassMappingsPath( '/absolute/path/to/vo/models' );
    attempts to include it, and instantiate an object of MyClass (therefore your filename MUST match your class name).
    i.e. $clazz = new $classname;

So I realised a problem. Constructors, both in Flash and PHP MUST at all costs have default values for them, i.e. the constructor variables are optional, because AMFPHP and Flash don’t use the sent objects, they literally convert them to their relevant objects on each side.

Further info on problems and debugging NetConnection.Call.BedVersion in Flash and AMFPHP can be found by google of course.

Cpamf accepting VO objects in service calls as parameters

Monday, October 5th, 2009

Here’s the status:
Cake implementation of AMFPHP via the plugin cpamf appears to be lacking some particular documentation regarding “accepting” flash objects in calls.
i.e.
Flash -> PHP object conversion.

Sure you can send back flash objects no problem, and there are plenty of documented places that tell you how to send flash objects back from PHP using class mapping and there was even some information on the reverse using AMFPHP on how to map VO’s from Flex to PHP using AMFPHP, but what seemed to be lacking was some particular documentation on how to get CPAMF, cake’s own brand of AMFPHP working with Flex VO to PHP. Begin investigation:

I am indeed hoping that I can get my controllers to accept objects instead of parameters or arrays. Allowing the flash app to pass through an object instead of having to decide what parameters or format it should be returning each time.

e.g.
Controller | Method | Parameter
TestController->test(StandardVO $vo)

As cake does; it likes to have controllers and models for everything it does. Aren’t you a nice cake! *grumble*
So it’s written it’s own CakeGateway.php class to extend the PHPAMF Gateway class, overriding action calls for the gateway. Aside from overriding them all and not calling the parent::registerActionChain() that seems to be fine.
A little hunt through the documentation/notes and general settings in globals.php seems to indicate that you can set the directory for controllers and vo classes. Great I think; changing these values should resolve my problem. If only!

The CakeGateway class appears to initialise the gateway with it’s own set of parameters for the controllers, sensibly (for a change) reading cake’s default controllers folder. Thus, all appears to be working within the /cpamf/browser , but still without the ability to pass objects, DOH!
The voPath mapping that appears to be set from the original globals file points to “services/vo”, i’m assuming this is supposed to indicate the current directory is the base and the services/vo folder is the one containing the vo’s. Thus, following some logical thinking I begin to stuck some classes in various folders that cake might just be reading.
In goes a StandardVo class, in the models/ directory and also inside the plugins/cpamf/vendors/amfphp/services/vo directory, try again. Nope still not recognising my object.
Ok, I know Cake’s naming convention is totally crap, lets try naming the file standard_vo.php instead of StandardVo.php . Still nothing, despite being implemented in both directories.

A few hours later, and after a fair amount of digging around in AMFPHP I came across what looked like some form of include()’ing. Having managed to stop the flash from doing it’s thing an instead die()’ing in certain places, I realised that this part here was responsible for loading in the PHP class to map to (lines 351-396 of /plugins/cpamf/vendors/amfphp/core/amf/io/AMFBaseDeserializer.php). Stepping through the logic I saw it: “$mappedClass = str_replace(’.', ‘/’, $typeIdentifier);”
The mapClass() function was doing the magic; interpreting the type of the flash object (which seemed to resolve to a full package name) into an include. AH HAH! It was also resolving it relative to the customMappingsPath which we saw earlier was set in CakeGateway class (cake_gateway.php).
So it would seem that AMFPHP was expecting to see a full package naming convention for all the Flash Objects it was going to receive.

I headed back to the CakeGateway class and amended the setCllassMappingPath() to

// This is the path to the Vo models!
$gateway->setClassMappingsPath( MODELS . 'vo' );

I then created a “vo” directory inside the cake models directory, and further proceeded to mimic the flash namespace package path.
models/vo/com/test/StandardVo.php , et VOILA! Problem resolved; cpamf now knows which object it is receiving and fills it with the data received from the flash object.

Now back to pulling out some hair over other issues.

CakePHP doesn’t like autoload() with vendor app import!

Tuesday, September 22nd, 2009

So, it would seem that if you decide that you want your vendor plugin to work nicely with cake, by having it conform to Pear folder directory structure and naming convention, and then right yourself a nice autoload() function to register with php (via spl_autoload_register()), you CANNOT use the App::import alongside this.
Why? Because Cake’s lovely App::import does a require() not a require_once()… Go figure!

So here is the problem:
You have App::import()’d your vendor plugin. It registers itself as an autoloader.
You have your 2nd vendor plugin, (which conforms to a Pear style structure also).
You explicitly App::import(’vendor’,'2nd item’,array(’file’=>’path/to/vendor/file.php’));
Cake has a fit and dies saying you “cannot redeclare “.
Brilliant Cake well done! Another reason to use Zend Framework…

As a solution I have altered the cake source code to make sure the require() is in fact a require_once(). I have yet to see the impact of such a change on any other import()’s. See below for alteration:
File: cake/libs/configure.php
Line: 975
Previous: “require($file);”
Change: “require_once($file);”

CakePHP CPAMF flex VO object flash integration

Monday, September 21st, 2009

Well thank god for that!

After a few days of stressing and pulling out hair, and with the help of one young dutch genius we managed to resolve the cake cpamf plugin VO integration.

Firstly, things to know about cake:

  1. Cake REQUIRES all models to be in the “models” directory.
  2. CPAMF plugin is installed in in the “plugins” directory.
  3. You can view all controllers/services at /cpamf/browser
  4. The gateway that flash connects to is /cpamf/gateway (not /cpamf/gateway.php as the default seems to think).
  5. CPAMF does NOT use the vendor/services/vo directory that is inside the amfphp part of the plugin.

Flash RemoteClass alias can be any path

e.g.

package
[RemoteClass(alias="org.test.TestOp")]
/**
* @author
*/
public class TestOP
{
public var username:String;
public var password:String;
public var id:int;
public function TestOP( username:String=null, password:String=null, id:int=0) {
this.username = username;
this.password = password;
this.id = id;
}
}
}

This class path MUST be represented in the PHP class using the _explicitType = ‘org.test.TestOp’. Once this is done you can pass back this class through any controller method.

e.g.

class TestOp {
public $_explicitType = 'org.test.TestOp';
public $username;
public $password;
public $id;
}

So, as you can see from the above, the PHP class MUST have the full package path to the flash class, and your class will be “models/test_op.php” as cake has a horrid non-pear style naming convention.

Eclipse PHP PDT no file extension problem.

Monday, December 8th, 2008

So to my surprise recently, Eclipse PHP PDT doesn’t show syntax highlighting for files which don’t have the .php (or variant of that) extension. i.e. it won’t allow a blank extension.

Honestly there is no reason not to use the .php extension if you’re working with a proper MVC framework, however in some recent work i have come across i’ve seen this used to facilitate “pretty” urls, which in turn has a positive influence on SEO ranking of course.

So, the solution is as follows. (more…)

Zend Loader isReadable causing trigger error problem

Monday, December 8th, 2008

I noted recently while writing a debugging error logging plugin for the Zend Framework that I was getting some E_WARNING’s logged in the database table it was utilising.

Having analysed the information a little further I realised that the framework was doing something rather funky. I had structured the application to be a modular layout as exemplified by the wiki guide for framework layout. I’d set up the module, controller and view components into their own directory (called “account”) and all was working fine. The Zend_Form was rendering correctly as I expected it to.

(more…)

How to get a better Google snippet description

Friday, July 11th, 2008

I suspect you’ve always wondered how google gets the snippet information that it displays with the link to your site in it’s SERPs. Well I have too, and now i’ll let you in on the secret http://googlewebmastercentral.blogspot.com/2007/09/improve -snippets-with-meta-description.html

“We want snippets to accurately represent the web result. We frequently prefer to display meta descriptions of pages (when available) because it gives users a clear idea of the URL’s content.”

So it seems that google does care about meta descriptions after all…

How do we apply this to dynamically generated products then I hear you ask. Well firstly I’m going to imagine that you have your product information stored in a database or flat file format and you know your SQL/parsing queries that will pull the relevant tags.

(more…)