Sven
Sven

Reputation: 132

Zend 3 - Change template basepath

I am just starting to migrate some of my applications from Zend#1 to Zend#3. All is working fine, but regarding the views I've some trouble understanding the underlying concept.

As the tutorials suggest my project layout is like this:

module
  Application
    view
      application
        index
          main.phtml
          foo.phtml
        baz
          index.phtml

I'm wondering why you need to dublicate the "application" folder inside the view directory - you are already in the directory hirachy of the module. Is there a way to change the search path for the default template resolver so that the module name is omited? Just relying on the viewManager's "template_path_stack" is not working. Do I really need to write a custom resolver here?

Thanks a lot!

PS. Nope, I do not want to use custom template maps here ;-) I want to understand and use the default revolver without template maps, if possible.

Upvotes: 0

Views: 419

Answers (2)

jcropp
jcropp

Reputation: 1246

"Application" is the name of a module in your overall 'application.' At the start, "Application" is the only module, but it is common to add other modules: you might have a module for "Clients" and another module for "Vendors." The hierarchy of the view folders follows the same hierarchy as ModuleName:ControllerName:ActionName, and ZF needs to use the module name in the view folders hierarchy in case you happen to have identical controller and action name pairs in two or more modules. It is likely that a "Clients" module and a "Vendors" module will both have an "index" action. It is less likely that the two would have identical controller names but it's not completely out of the question. If you had a controller named "Contacts" in both a "Clients" module and a "Vendors" module, "contacts/index" isn't enough information to tell ZF which view to use. It needs the module name in the folder hierarchy to distinguish between "clients/contacts/index" and "vendors/contacts/index".

UPDATE

Something to wrap your head around is that ZF3 takes things like router definitions, view folders and who knows what else from all of your different modules and aggregates them into a single structure. In other words,

module
  Application
    view
      application
        add
          add.phtml          
        delete
          delete.phtml          
        edit
          edit.phtml          
        index
          index.phtml          

module
  Clients
    view
      clients
        add-client
          add.phtml          
        delete-client
          delete.phtml          
        edit-client
          edit.phtml          
        client-index
          index.phtml          

module
  Vendors
    view
      vendors
        add-vendor
          add.phtml          
        delete-vendor
          delete.phtml          
        edit-vendor
          edit.phtml          
        vendor-index
          index.phtml

gets recognized somewhat like this:

module
  ....
    view
      application
        add
          add.phtml          
        delete
          delete.phtml          
        edit
          edit.phtml          
        index
          index.phtml          
      clients
        add-client
          add.phtml          
        delete-client
          delete.phtml          
        edit-client
          edit.phtml          
        client-index
          index.phtml          
      vendors
        add-vendor
          add.phtml          
        delete-vendor
          delete.phtml          
        edit-vendor
          edit.phtml          
        vendor-index
          index.phtml

and you could probably put all of your view files into a single module if you wanted to.

Perhaps this helps explain why a folder with the module name is included beneath the "view" folder. The folder with the module name that is above the "view" folder has a storage function. The folders with module names below the "view" folder serve as a means of referencing which modules the view files are associated with in the aggregated definition.

Upvotes: 1

Sven
Sven

Reputation: 132

Found one possible solution!

In your module's config add this one to the view_manager::

'controller_map' => [
    'Dashboard\Controller\DashboardController' => 'Dashboard'
],

Instead of looking for a template called "Dashboard/view/dashboard/[controller]/[action].phtml" the framework will now look for "Dashboard/view/[controller]/[action].phtml". Basically you are telling Zend to use some kind of shorthand here and strip the array's value from the template resolution (have a look at InjectTemplateListener::mapController())

Anyways... a better solution and explanation is welcome!

Upvotes: 1

Related Questions