この 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
(「ホームルート」内でのみ)リクエストパラメーターを取得できました...