2
votes

I'm trying to setup some routes for my ZF app but not getting too far. I have a controller 'WebServiceController', it has an index action and a lookupTransaction action. I want to use routes like this:

ws/

ws/lookupTransaction

Ideally I'd like anything with a 'ws/' prefix to go to the WebServiceController and match the action name. I'm not sure how to do that yet but I am trying to get each route working so I added these two routes:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    function _initRoutes()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();

        $router->addRoute('ws', new Zend_Controller_Router_Route('ws/', array(
            'controller'    =>  'web-service',
            'action'        =>  'index',
        )));

        $router->addRoute('ws/lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
            'controller'    =>  'web-service',
            'action'        =>  'lookup-transaction',
        )));
    }

}

The first one works as expected but the second one doesn't, I just get 'Application Error'. What am I doing wrong? Just out of interest, if I remove my two routes and try and go to:

web-service/lookup-transaction

I still get the same error!

Solved

Here is how I can make it work with camel cased action name and camel cased URL.

$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
    'controller'    =>  'web-service',
    'action'        =>  'index',
)));
$router->addRoute('ws-lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
    'controller'    =>  'web-service',
    'action'        =>  'lookup-transaction',
)));

Thanks

Ziad

2
And does your WebServiceController have a lookupTransactionAction?wimvds
BTW You could turn on error logging and check the logs for the specific cause (could be that you have the action defined but haven't defined a view/disabled rendering on the view, which will raise an exception).wimvds
Thanks wimvds, I do have the action but I didn't have the environment setup properly so was not getting the full error messages, that's fixed now.Ziad
Just wondering: doesn't it make more sense to configure the custom routes with the actual controller name and action name instead of the url'd versions?Ziad

2 Answers

0
votes

Try just this one route as a solution to both problems:

$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
    'controller'    =>  'web-service',
    'action'        =>  'index',
)));

the action parameter then serves as a default, so if no action is specified in the URL, index will be used. Otherwise it will route to the action in the URL. So example.com/ws/lookupTransaction will go to lookuptransactionAction() in your controller.

If this still gives you an error, post the error message so we can see what the problem is.

0
votes

The router actually transforms URLs to lowercase. So the correct URL should be all lowercase dash separated words. Also I'm not sure if it's possible to use slash in route name (the first parameter of addRoute()).