コントローラー クラスを拡張Zend_Controller_Action
し、次の変更を加えました。
メソッドでdispatch($action)
置換
$this->$action();
と
call_user_func_array(array($this,$action), $this->getUrlParametersByPosition());
そして、次のメソッドを追加しました
/**
* Returns array of url parts after controller and action
*/
protected function getUrlParametersByPosition()
{
$request = $this->getRequest();
$path = $request->getPathInfo();
$path = explode('/', trim($path, '/'));
if(@$path[0]== $request->getControllerName())
{
unset($path[0]);
}
if(@$path[1] == $request->getActionName())
{
unset($path[1]);
}
return $path;
}
今のようなURLのために/mycontroller/myaction/123/321
私のアクションでは、コントローラーとアクションに続くすべてのパラメーターを取得します
public function editAction($param1 = null, $param2 = null)
{
// $param1 = 123
// $param2 = 321
}
URL に追加のパラメーターを指定してもエラーは発生しません。これは、定義したメソッドに追加のパラメーターを送信できるためです。それらはすべて取得でき、通常の方法でfunc_get_args()
使用することもできます。getParam()
URL には、デフォルトのものを使用するアクション名が含まれていない可能性があります。
実際、私の URL にはパラメーター名が含まれていません。それらの値のみ。(問題のとおりです)そして、フレームワークの概念に従い、Zendメソッドを使用してURLを構築できるようにするには、ルートを定義してURLのパラメーター位置を指定する必要があります。しかし、URL 内のパラメーターの位置を常に知っている場合は、このように簡単に取得できます。
これは、リフレクション メソッドを使用するほど洗練されていませんが、オーバーヘッドが少ないと思います。
Dispatch メソッドは次のようになります。
/**
* Dispatch the requested action
*
* @param string $action Method name of action
* @return void
*/
public function dispatch($action)
{
// Notify helpers of action preDispatch state
$this->_helper->notifyPreDispatch();
$this->preDispatch();
if ($this->getRequest()->isDispatched()) {
if (null === $this->_classMethods) {
$this->_classMethods = get_class_methods($this);
}
// preDispatch() didn't change the action, so we can continue
if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) {
if ($this->getInvokeArg('useCaseSensitiveActions')) {
trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"');
}
//$this->$action();
call_user_func_array(array($this,$action), $this->getUrlParametersByPosition());
} else {
$this->__call($action, array());
}
$this->postDispatch();
}
// whats actually important here is that this action controller is
// shutting down, regardless of dispatching; notify the helpers of this
// state
$this->_helper->notifyPostDispatch();
}