Archived entries for cakephp

CakePHP AMF /w Flex

After several hours of tests and deploys, I’ve managed to figure out a step-by-step guide to connect cakePHP to Flex3 through a AMF gateway. Please follow the steps closely, and if you have any questions, feel free to leave me a message. Good luck!

Update [May 20th, 2009] : This step-by-step guide is based on tools & tutorials made by Daniel from www.carrotplant.com

  1. Download flex example: http://carrotplant.com/en/blog/cpamf-flex-example
  2. Download php example: http://carrotplant.com/en/blog/cpamf-php-example
  3. Download CakePHP: http://cakephp.org
  4. Extract CakePHP in a folder ( lets say path_to_your_cakephp_folder )
  5. Edit hosts file ( /etc/hosts or C:\WINDOWS\system32\drivers\etc in most cases )
    1
    127.0.0.1       amf.local
  6. Edit httpd.conf ( Apache configuration file )
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <VirtualHost *:80>  
        DocumentRoot C:/path_to_your_cakephp_folder/app/webroot
        ServerName amf.local

        <Directory "C:/path_to_your_cakephp_folder/app/webroot">
            Options Indexes FollowSymLinks Includes ExecCGI
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>  
    </VirtualHost>
  7. Setup Security.Salt in app/config/core.php and database settings in app/config/databases.php
  8. Execute cpamf.sql from cpamf-php-example
  9. Copy from cpamf-php-example into your path_to_your_cakephp_folder folder:
    • app/controllers/users_controller.php
    • app/models/user.php
    • app/plugins/cpamf/*
  10. Modify app/plugins/cpamf/vendors/amfphp/core/cakeamf/util/CakeMethodTable.php at line 74 with following:
    1
    2
    3
    $className = str_replace('\', '/', $className);
    $className = substr($className, strrpos($className, '
    /') + 1);
    $sourcePath = $servicePath . Inflector::underscore( $className ) . ".php";
  11. Access http://amf.local/cpamf/browser and set path of gateway to http://amf.local/cpamf/gateway (without .php at end)
  12. Create folder app/webroot/flex, and create a Flex 3 Project called amf.local with Flash Develop
  13. Download the latest version of Mate AS3 Flex Framework from http://code.google.com/p/mate-framework/ (eg.: Mate_08_7.swc)
  14. Put the Mate SWC file into the LIB folder of your Flex project ( app/webroot/flex/lib )
  15. Put content of cpamf-flex-example/src into the SRC folder of your Flex project ( app/webroot/flex/src )
  16. Put services-config.xml into the root of project ( app/webroot/flex )
  17. Edit services-config.xml at line 21, to point to the right gateway
    1
    2
    3
    <endpoint
        uri="http://amf.local/cpamf/gateway"
        class="flex.messaging.endpoints.AMFEndpoint" />
  18. Put the whole code of cpamf_test.mxml into Main.mxml, and keep cpamf_test.mxml for future uses
  19. Set properties for project ( right click on the project name, and then click on Properties )
    • Compiler Options » Additional Compiler Options » SET : -services=services-config.xml
    • Compiler Options » SWC Include Libraries » SET : lib/
    • Compiler Options » SWC Libraries » SET : lib/
  20. Test/Build the project and then you can access your swf at http://amf.local/flex/bin/amflocal.swf
  21. Enjoy! :)

A branch from the Tree Behavior

Update 2009.04.03: I made a few mistakes on the initial post. Code modifications were updated.

Last days I got busy figuring out a way to generate a select box in CakePHP which looks like the one generated by the generateTreeList() method from Tree Behavior. Why complicating things ?! Because I wanted only a part from that tree, and because generateTreeList() generates a select box from the whole table and not from a branch within that table. For example, we could have the folowing tree:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    Departments [ 1 - 30 ]
        Management [ 2 - 7 ]
        Financial [ 3 - 4 ]
        HR [ 5 - 6 ]
        Marketing [ 8 - 9 ]
        Production [ 10 - 27 ]
            Java [ 11 - 16 ]
                Front End Developing [ 12 - 13 ]
                Back End Developing [ 14 - 15 ]
            Flash [ 17 - 18 ]
            Design [ 19 - 24 ]
                Web Design [ 20 - 21 ]
                Print Design [ 22 - 23 ]
            QA [ 25 - 26 ]
        Support [ 28 - 29 ]

Ok. So let’s say that we want the whole tree. That’s way too simple. You just have to add the following lines into your controller action.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

class ApplicantsController extends AppController
{
    ...
    var $helpers = array ('Tree');
    ...
    function show_select_box()
    {
        ...
        $this->set('departments', $this->Department->children($id));
        ...
    }
    ...
}

?>

And the result should be sent to the following template:

1
2
3
4
5
6
7
<?php

    ...
    select('departments', $departments, null, array(), true);
    ...

?>

What if you want to create a select box starting with the node Production ?! What are the alternatives using the basic API from CakePHP 1.2 ? NONE!
So, here is my solution.

  1. download the Tree Helper from the Bakery
  2. to the following modifications at the proper lines of code (watch the line codes)
  3. 168
    169
    170
    171
    172
    173
    174
    <?php

        ...
        if ($type && $type !== 'select') { // [[   && $type !== 'select'  ]] added
        ...

    ?>
    171
    172
    173
    174
    175
    176
    177
    <?php

        ...
        if ($itemType && $itemType !== 'option') { // [[   && $itemType !== 'option'  ]] added
        ...

    ?>
    235
    236
    237
    238
    239
    240
    241
    242
    <?php

        ...
        if ($__addType && $hasChildren  && count($stack) == 0) {
        // [[  && $hasChildren && count($stack) == 0  ]] added
        ...

    ?>
    250
    251
    252
    253
    254
    255
    256
    257
    <?php

        ...
        $return .= '<' . $itemType . $itemAttributes . '>'.str_repeat('--', count($stack));
        // [[  .str_repeat('--', count($stack))  ]] added
        ...

    ?>
    267
    268
    269
    270
    271
    272
    273
    <?php

        ...
        $return .= '</' . $itemType . '>'; // added the whole line
        ...

    ?>
  4. add this element to views/elements as department_selectbox_option.ctp
  5. 1
    2
    3
    4
    5
    6
    7
    8
    <?php

        $tree->addTypeAttribute('name', 'data[Applicants][departments]', null, 'previous');
        extract($data);
        $tree->addItemAttribute('value', $Department['id']);
        echo $Department['name'];

    ?>
  6. edit the action’s template file ( views/show_select_box.ctp ) like this
  7. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?php

                echo $tree->generate($departments, array(
                    'model' => 'Department',
                    'type' => 'select',
                    'itemType' => 'option',
                    'element' => 'department_selectbox_option',
                    'id' => 'ApplicantsDepartments',
                    'name' => 'data[Applicants][departments]'));

    ?>

And voila! If we would like to see the departments under the Production department, then you should get a select box indented like the following list :)

1
2
3
4
5
6
7
8
9
Production [ 10 - 27 ]
    Java [ 11 - 16 ]
        Front End Developing [ 12 - 13 ]
        Back End Developing [ 14 - 15 ]
    Flash [ 17 - 18 ]
    Design [ 19 - 24 ]
        Web Design [ 20 - 21 ]
        Print Design [ 22 - 23 ]
    QA [ 25 - 26 ]

For questions & comments please leave me feedback! Good luck!



Copyright © 2004–2009. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses Modern Clix, a theme by Rodrigo Galindez.