views:

246

answers:

2

Here is my specific issue. I want to make an api level which then under that you can select which method you will use. For example:

test.com/api/rest

test.com/api/xmlprc

Currently I have api mapping to a module directory. I then setup a route to make it a rest route. test.com/api is a rest route, but I would rather have it be test.com/api/rest. This would allow me later to add others.

In Bootstrap.php:

        $front = Zend_Controller_Front::getInstance();
  $router = $front->getRouter();
    $route = new Zend_Controller_Router_Route(
 'api/:module/:controller/:id/*',
 array('module' =>'default')

);

$router->addRoute('api', $route);

$restRoute = new Zend_Rest_Route($front, array(), array( 'rest' )); $router->addRoute('rest', $restRoute);

return $router;

In application.ini:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

I know it will involve routes, but sometimes I find the Zend Framework documentation to be a little hard to follow.

When I go to test.com/rest/controller/ it works how it should, but if I go to test.com/api/rest/ it tells me that my module is api and controller is rest.

A: 

Your orginal line was this.

$restRoute = new Zend_Rest_Route($front, array(), array( 'rest' ));

To enable Zend_Rest_Route for specific controllers, add an array of controller names as the value of each module array element.

$restRoute = new Zend_Rest_Route($front, array(), array( 'api' ) => array('rest'));

Also reference http://framework.zend.com/manual/en/zend.controller.router.html

I hope this may help.

Chris
I actually want anything to api/rest/:controller to be rest. so api/rest/user and api/rest/differentresource both to be REST. I want to have a dummy api folder up front so that I can offer other services.
Josh
A: 

You might actually want to do something like api/:controller/:action.json or api/:controller/:action.xml (i've seen alot of API's do this, ex: Twitter).

To achieve this you can do something like this:

$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter(); // returns a rewrite router by default
$router->addRoute(
            'json_request',
            new Zend_Controller_Router_Route_Regex(
                                            '([^-]*)/([^-]*)/([^-]*)\.json', 
                                            array(
                                                'controller'   => 'index',
                                                'action'       => 'index',
                                                'request_type' => 'json'),
                                            array(
                                                1 => 'module',
                                                2 => 'controller',
                                                3 => 'action'
                                            )
        ));

Then just check your parameter "request-type" and serve the request based on what you have. You should change your layout to enconde JSON or XML based on your request also.

You would need an API module also with this.

Note: Take care that the bootstrap of a module is added to ALL of your modules currently so you would have this route run in all modules. I am currently checking for a way to fix this so cannot tell you how to do it.

Hope it helped!

Andrei Gabreanu