简体   繁体   中英

How to change the controller class name in Zend Framework

For example, we have index controller:

class IndexController{ ...

How to make eg

class Index_Controller{ ...

work?

I've never tried it myself (and wouldn't really recommend doing this : knowing how classes are nammed is nice, because they follow the standard, when you start working on an existsing ZF-based project !) , but there might be a solution ; see the following section from the manual : 12.6.2. Subclassing the Dispatcher

At the bottom of the page, it says (quoting, emphasis mine) :

Possible reasons to subclass the dispatcher include a desire to use a different class or method naming schema in your action controllers , or a desire to use a different dispatching paradigm such as dispatching to action files under controller directories (instead of dispatching to class methods).

So, this might help you do what you're asking -- once again, I've never this seen done, so I can't say for sure, but I hope this will give a hint to the final solution...

As Pascal MARTIN suggested, subclassing the dispatcher is the right way to go - even I'd share his opinion that deviating from the given ZF-naming convention should be well-founded.

Zend_Controller_Dispatcher_Abstract provides two methods that determine the name of the controller class and the action method respectively:

public function formatControllerName($unformatted)
{
    return ucfirst($this->_formatName($unformatted)) . 'Controller';
}

public function formatActionName($unformatted)
{
    $formatted = $this->_formatName($unformatted, true);
    return strtolower(substr($formatted, 0, 1)) . substr($formatted, 1) . 'Action';
}

You can just subclass Zend_Controller_Dispatcher_Abstract and override one or both methods to match your required naming convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM