views:

69

answers:

2

Hello everybody. I'm struggling with the Zend-Router. I want to chain language in my url. Everything works fine but my modular routing.

If i call: http://domain.de/en/index - the indexAction of my IndexController of the default module is executed and translated.

Same goes for: http://domain.de/en/about So the aboutAction of the IndexController is called.

If i call: http://domain.de/en/forum/index it should execute the indexAction of the IndexController of the forum module. But it does not.

My aim is to shorten my urls as good as possible, so there is no "default" or "index" in it.

Can you help me editing my routes.xml so i get the wanted results?

my routes.xml

<config>
    <routes>
        <sc1 type="Zend_Controller_Router_Route">
            <route>:lang/:@module/:@controller/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc1>
        <sc2 type="Zend_Controller_Router_Route">
            <route>:lang/:@module/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc2>
        <sc3 type="Zend_Controller_Router_Route">
            <route>:lang/:@controller/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc3>
        <sc4 type="Zend_Controller_Router_Route">
            <route>:lang/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc4>
    </routes>
</config>

Do you have an idee?

Thank you in advance, Tobias

+1  A: 

I am not too great at Zend Routes, but looking at your code you need a way to distinguish what module it should go to. You have it set for default for all occurrences. It would be nice if it could be dynamic like you have it, but I think you will need to setup routes specifically for forums etc. Given the Zend needs to know where to send it to. If you can figure out how to use the :@module variable to send it to that module, that would work but I do not know / think that is possible.

After reading through the manual I came up with this structure for you. You will have to define each item, such as forums, you want it to redirect to as show below.

<config>
    <routes>
        <language type="Zend_Controller_Router_Route">
            <route>:language</route>
            <reqs language="[a-z]{2}">
        </language>
        <index type="Zend_Controller_Router_Route_Static">
            <route></route>
            <defaults module="default" controller="index" action="index" />
        </index>
        <about type="Zend_Controller_Router_Route_Static">
            <route>about</route>
            <defaults module="default" controller="index" action="about" />
        </about>
        <forums type="Zend_Controller_Router_Route_Static">
            <route>forums</route>
            <defaults module="forums" controller="index" action="index" />
        </forums>
        <lang-forums type="Zend_Controller_Router_Route_Chain">
            <chain>language, forums</chain>
        </lang-forums>
        <lang-about type="Zend_Controller_Router_Route_Chain">
            <chain>language, about</chain>
        </lang-about>
    </routes>
</config>

I am not 100% sure about the about portion, specifically the chaining, but messing with it should give you the correct way.

EDIT

The below is left in tact just as another possible example. I think I have the correct answer above.

Looking at the Zend Routes Manual it is very confusing on how to set it up. I also do not like working in the xml format, I prefer the .ini, but here is a "psuedo mock up" of how it should work (untested, given it would be hard to test):

lang.index.type = "Zend_Controller_Router_Route"
lang.index.route = ":language"
lang.index.defaults.controller = index
lang.index.defaults.action = index

lang.forums.index.type =  "Zend_Controller_Router_Route_Static"
lang.forums.index.route =  "forums"
lang.forums.index.controller =  forums
lang.forums.index.action =  index

lang.forums.register.type =  "Zend_Controller_Router_Route_Static"
lang.forums.register.route =  "forums/register"
lang.forums.register.controller =  forums
lang.forums.register.action =  register

lang.forums.login.type = "Zend_Controller_Router_Route_Static"
lang.forums.login.route = "forums/login"
lang.forums.login.controller = forums
lang.forums.login.action = login

lang.forums.view.type = "Zend_Controller_Router_Route_Static"
lang.forums.view.route = "forums/thread/:slug"
lang.forums.view.controller = forums
lang.forums.view.action = view

lang.other.index.type = "Zend_Controller_Router_Route_Static"
lang.other.index.route = "other"
lang.other.index.controller = other
lang.other.index.action = index

lang.other.view.type = "Zend_Controller_Router_Route_Static"
lang.other.view.route = "other/:slug"
lang.other.view.controller = other
lang.other.view.action = view

Hopefully that gets you pushed with the right line of thinking of what you need to do. If someone else knows how to do it dynamically, I would totally be interested! I will work on the xml method and see if I cannot decipher how to do it properly from the abysmal documentation.

Brad F Jacobs
But isn't there a better way than declaring everything by hand?I mean the modular route works just fine. Only when i want to chain the language, my modules arent't accessible anymore.
Tobias Kun
Not that I know of. If you can test / figure it out I would be interested in knowing. The only way I could see that possible is using the variable in the declaration, IE: `<module>@:module</module>` but I highly doubt that is possible.
Brad F Jacobs
A: 

If i only use

<config>
    <routes>
        <sc1 type="Zend_Controller_Router_Route">
            <route>:lang/:@module/:@controller/:@action</route>
            <defaults>
                <lang>de</lang>
                <module>default</module>
                <controller>index</controller>
                <action>index</action>
            </defaults>
        </sc1>
    </routes>
</config>

in my routes.xml, the modular routing, the url translation and language chaining work as expected. But now the default module and index controller are visible too.

(http::/domain.de/en/default/index/index)
(http::/domain.de/en/default/index/about)
(http::/domain.de/en/forum/index/index)

Does anyone have an idea to shorten it?

Thanks, Tobias

Tobias Kun