Posts Tagged Zend

Getting started with zfc-user-doctrine-orm

zfc-user-doctrine-orm Is a Zend Framework 2 Module that enables Doctrine 2 support for the ZfcUser module.

Basic Setup

Installation via composer:

composer require "zf-commons/zfc-user-doctrine-orm:0.1.*"

An appropriate version of ZfcUser should be pulled in during installation.

Configuration Setup

  • Copy vendor/zf-commons/zfcuser.global.php.dist to config/autoload/zfcuser.global.php
  • Edit your application.config.php to include
    modules => array('ZfcBase',
            'ZfcUser',
            'ZfcUserDoctrineORM')

You don’t have to edit zfcuser.global.php – the default values will work fine, and zfc-user-doctrine-orm will inject itself such that you don’t even have to edit the zend_db_adapter (as I initially thought you would).

Custom User Entity

If you wish to use a custom user entity definition (too add additional fields, for example).

To do this, modify your zfcuser.gloabl.php to include the following configuration values:

'zfcuser' => array(
     'UserEntityClass' => '\UserEntityNamespace\UserEntityClass',
     'EnableDefaultEntities' => false
),

, , , ,

No Comments

Reset Zend Server password on OpenShift

If you are using the Zend Zerver Installation on the OpenShift cloud and happen to forget your password, you may find yourself wondering where gui_passwrd.sh is located.

When you ssh into your openshift gear, you find that the zend/bin folder does not include the gui_passwrd.sh script. This is because this folder stores the openshift scripts to install / maintain / remove the zend server itself. The Zend Server’s bin folder is located at zend/usr/local/zend/bin

#To Change your password, login to your openshift instance and enter the following command
zend/usr/local/zend/bin/gui_passwd.sh

Alternatively, use the new configuration utility, as gui_passwd.sh is deprecated:

zend/usr/local/zend/bin/zs-setup set-password <password>

, , , ,

No Comments

Getting started with AbstractRestfulController

Zend Framework 2 introduced a new controller abstract class: AbstractRestfulController (more docs here). This is used to create restful services (which will communicate with javascript in the browser, for example).

Controllers implementing AbstractRestfulController, can expose normal actions in addition to the REST methods. Following the Zend convention, these the name of these methods must end in “Action”.

Example Rest Controller, with only the getList() method populated.

class TestRestController extends AbstractRestfulController
{
    public function indexAction()
    {
        return new ViewModel();
    }
 
    /**
     * Return list of resources
     *
     * @return mixed
     */
    public function getList()
    {
        return new JsonModel(array(
            array('name' => 'test'),
            array('name' => 'second')
        ));
    }
 
    /**
     * Return single resource
     *
     * @param  mixed $id
     * @return mixed
     */
    public function get($id)
    {
        //TODO: Implement Method
    }
 
    /**
     * Create a new resource
     *
     * @param  mixed $data
     * @return mixed
     */
    public function create($data)
    {
        //TODO: Implement Method
    }
 
    /**
     * Update an existing resource
     *
     * @param  mixed $id
     * @param  mixed $data
     * @return mixed
     */
    public function update($id, $data)
    {
        //TODO: Implement Method
    }
 
    /**
     * Delete an existing resource
     *
     * @param  mixed $id
     * @return mixed
     */
    public function delete($id)
    {
        //TODO: Implement Method
    }
}

I found the dispatch logic for AbstractRestfulController a little confusing.
If there is an available “action” value on the request object, the controller attempts to find the corresponding action method. If there is no action value, then it dispatches to the REST methods. This essentially means that we will have to setup custom routes to dispatch to AbstractRestfulControllers as the “action” value will typically have a default value of “index” if we are following the traditional route configuration.

If you are getting 404 Not Found errors, when you are expecting the REST methods to be executing, this is probably the problem.

Example route configuration (In module.config.php):

return array(
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\TestRest' => 'Application\Controller\TestRestController',
        ),
    ),
 
    'router' => array(
        'routes' => array(
            'rest' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/rest',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/:controller[/:id][/]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),

To get the controller to just render the JSON in the JsonModel returned by the getList method, we have to add a view manager strategy to the config:

return array(
    'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
        'strategies' => array(
            'ViewJsonStrategy',
        ),
    ),
);

, ,

3 Comments