1

この zf2 ルート構成を標準のスケルトン アプリケーションに追加しました: (編集: ここで私の完全な構成:)

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),
        'test' => array(
                'type'    => 'literal',
                'options' => array(
                    'route'    => '/test',
                    'defaults' => array(
                           'controller' => 'Application\Controller\Index',
                           'action'     => 'test',
                    ),
                ),
                'may_terminate' => true,
                'child_routes'  => array(
                    'query' => array(
                        'type' => 'Query',
                        'options' => array(
                            'defaults' => array(
                                'testparam' => 'bar'
                            ),
                        ),
                    ),
                ),    
        ),

        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            //'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),

    ),
),

コントローラーで、testAction内のリクエストパラメーターを取得したい:(編集:ここで私の完全なコントローラー):

    namespace Application\Controller;

    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;

    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
            var_dump(
                    $this->params()->fromQuery(),
                    $this->getRequest()->getQuery(),
                    $this->getEvent()->getRouteMatch()->getParams()
             );

            return new ViewModel();
        }

        public function testAction(){
            var_dump(
                    $this->params()->fromQuery(),
                    $this->getRequest()->getQuery(),
                    $this->getEvent()->getRouteMatch()->getParams()
             );

            return new ViewModel();
        }
    }

URL: /test?testparam=123123
結果:パラメータが正しくありません!
ここで、may_terminate を削除すると、PARAM が「123123」になると予想されます。

PARAM = "bar" (see default value for "testparam")

array(0) { } object(Zend\Stdlib\Parameters)#195 (1) { ["storage":"ArrayObject":private]=> array(0) { } } array(3) { ["controller"]=> string(28) "Application\Controller\Index" ["action"]=> string(4) "test" ["testparam"]=> string(3) "bar" }

URL: /?testparam=123123
結果: PARAM は OK です!

array(1) { ["testparam"]=> string(6) "123123" } object(Zend\Stdlib\Parameters)#88 (1) { ["storage":"ArrayObject":private]=> array(1) { ["testparam"]=> string(6) "123123" } } array(2) { ["controller"]=> string(28) "Application\Controller\Index" ["action"]=> string(5) "index" } 

これらはどちらも機能しません。次の URL で NULL 値のみを受け取ります。/test?testparam=testvalue

このリクエストを行うと、機能します:/?xxx=xycvxcv

(ただし、内からパラメータを取得する必要がありますtestAction()

私はスケルトンアプリケーションの新規インストールを行いました->同じ結果(indexAction(「ホームルート」内でのみ)リクエストパラメーターを取得できました...

4

2 に答える 2

0

フロールーティングを使用している場合は、最初と2番目の方法が機能するはずです。

'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            ),

URL: http://localhost/project/public/?ad=23

var_dump:

配列'ad'=>文字列'23'(長さ= 2)

object(Zend \ Stdlib \ Parameters)[106] public'ad' => string '23'(length = 2)

*編集*:配列から'may_terminate'句を削除する必要があるかもしれません

于 2013-03-15T14:37:45.090 に答える
-1

   Query qoute 部分 (Zend\Mvc\Router\Http\Query) は非推奨です
    設定を次のように変更します
     'テスト' => 配列(
                'タイプ' => 'リテラル',
                'オプション' => 配列(
                    'ルート' => '/テスト',
                    'デフォルト' => 配列(
                           'コントローラー' => 'アプリケーション\コントローラー\インデックス',
                           'アクション' => 'テスト',
                    )、
                )
   

于 2014-12-11T05:52:18.143 に答える