0

I have a project witch I started without using a module structure in ZF 1 now I need to place a module structure for managing users (makes more sense to me).

My problem the "default" route should try to go with :controller/:action/:id and the module route
should go with :module/:controller/:action/:id the default from ZF 1 is to use the above without the :id
but my "logic" requires the :id field, how can I make this work?

What I've trying came to:

    protected function _initModuleAutoload()
{
    $modelLoader = new Zend_Application_Module_Autoloader(
                    array('namespace' => 'DM',
                        'basePath' => APPLICATION_PATH . '/modules/default')
                    , array('namespace' => 'UM',
                'basePath' => APPLICATION_PATH . '/modules/users')
    );

    $modelLoader->addResourceType('service', 'services', 'Service');
    $modelLoader->addResourceType('serviceplugin', 'services/plugins', 'Service_Plugin');

    return $modelLoader;
}

public function _initFrontController()
{
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();

    $route1 = new Zend_Controller_Router_Route(
                    ':module/:controller/:action/:id/',
                    array(
                        'id' => '\d+'
                        , 'module' => 'default'
                        , 'controller' => 'index'
                        , 'action' => 'index'
                    )
    );

    $router->addRoute('default', $route1);

    $front->addModuleDirectory(APPLICATION_PATH . "/modules/");

    $front
            ->registerPlugin(new Far_Access_Plugin_Identity(), 1)
            ->registerPlugin(new Far_Access_Plugin_Access(), 2)
            ->throwExceptions(true)
    ;

    return $front;
}

Also tried to create a second route for users instead of default but did not work.

Any ideas? What am I doing wrong?

A help from irc in freenode was given in channel #zftalk when I provided this link.

Bittarman: crash82: add requirements to the id, so it has to be [\d]+
Bittarman: or, just add an instance of Zend_Controller_Router_Route_Module chained to Zend_Controller_Router_Route with just :id in it, with a default set for id like false.
Bittarman: also, _initModuleAutoload, is pointless Bittarman: having your 'default' module in the modules dir, is kinda wrong
Bittarman: and you stop the frontcontroller resource from working by having an _initFrontController
Bittarman: so resources.frontController will no longer work.

crash82: hum... So many problems :( , so I can just place the "default" module into the application directory and any other modules can continue to be loaded from the modules / path ?

Bittarman: yes :)

crash82: going to try that Bittarman: and, each module which has a bootstrap, supplies its own module resource loader
Bittarman: so, if you go creating more like that, you end up with two for each module.
Bittarman: you'd be surprised how many people do tht.
Bittarman: s/tht/that/

Fernando André
  • 1,213
  • 3
  • 19
  • 32
  • 1
    The default routing in the current version of ZF always tests for module when routing. Adding a module will not effect routing if you are using default routes. Just add the module as the first parameter and it should work. if you want, check out thecode in `Zend_Controller_Router_Route_Module` – RockyFord Apr 30 '12 at 04:49
  • Yes that is part of the answer. :-) Thank you – Fernando André Apr 30 '12 at 21:32

1 Answers1

0

When I need to pass a parameter in a url in my scripts I use one of a couple of methods. For most simple things there is no real need to create a new route.

  1. <a href="/music/index/album/id/<?php echo $variable->id ?>"><?php echo $track->album ?></a>. Pass the parameter as /id/album_id
  2. use the url() view helper : <?php echo $this->url(array('controller' => 'index', 'action' => 'index', 'id' => $variable->id)); ?> This helper will also take a module as the first parameter.
  3. The first 2 work in view scripts, to do the same thing in a controller you can use the reidrector action helper: $this->getHelper('Redirector')->gotoSimple('action'=> 'someAction', 'controller' => 'someController', 'module' => 'someModule', array('id' => $id ); with controller, module and params being optional with a default of NULL.
  4. or you can use use the action utility method _forward() : $this->_forward($action, $controller, $module, array($params)) this same syntax as gotoSimple() has.

There are other ways to accomplish this kind of routing, I'll leave it to you to find the rest, but you might start with Action Helpers, utility methods and view helpers

RockyFord
  • 8,529
  • 1
  • 15
  • 21