12

ZF2ルーターの構成に関するヘルプを探しています。

開発コンテキストで単一のモジュールに複数のコントローラーを追加したいのですが、最終的には特定のルートを指定する方法を理解する必要があります。

これを行うために、私は現在、MVCクイックスタートドキュメントで指定されている汎用module.configを使用しようとしています。ただし、これはZF2のドキュメントが示唆するようには機能しないようです。

http://framework.zend.com/manual/2.0/en/modules/zend.mvc.quick-start.html

ドキュメントノート:

「ZendSkeletonApplicationには、このアクションに到達する可能性が高い「デフォルトルート」が付属しています。このルートでは、基本的に「/ {module} / {controller} / {action}」が必要です。これにより、次のように指定できます:「/ zend-user / hello / world」。明示的なルートを作成することをお勧めします。ここでは、主に説明のためにルートを作成します。」

ただし、コントローラーは/ landlord、/ landlord / home / indexをレンダリングしますが、/ landlord / sandbox/indexはレンダリングしません。ホームとサンドボックスはコントローラーです。Sandboxは、命名規則「SandboxController」に沿って命名されています。私の考えでは、おそらくドキュメントのコードのchild_routesセクションには、私が見逃したある種の変更が必要です。

サンドボックスインスタンスでは、404エラーページでこのエラーが発生します。

Landlord \ Controller \ Sandbox(無効なコントローラークラスまたはエイリアスに解決:Landlord \ Controller \ Sandbox)

'Landlord \ Controller \ Sandbox' =>'Landlord \ Controller \ SandboxController'を呼び出し可能オブジェクトに追加しようとしましたが、エラーが発生します。

私のコントローラーは以下のとおりです。

return array(
'controllers' => array(
    'invokables' => array(
        'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',

    ),
),
'router' => array(
    'routes' => array(
        'landlord' => array(
            'type'    => 'Literal',
            'options' => array(
                // Change this to something specific to your module
                'route'    => '/landlord',
                'defaults' => array(
                    // Change this value to reflect the namespace in which
                    // the controllers for your module are found
                    '__NAMESPACE__' => 'Landlord\Controller',
                    'controller'    => 'home',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // This route is a sane default when developing a module;
                // as you solidify the routes for your module, however,
                // you may want to remove it and replace it with more
                // specific routes.
                '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'landlord' => __DIR__ . '/../view',
    ),
),
);

URLを直接構成する簡単な方法がある場合は、これも役立ちます。または、これも非常に優れたチュートリアルであるとよいでしょう。

4

4 に答える 4

26

ここでの問題は、ルーターが追加した新しいコントローラーにルーティングしようとしても、追加するすべてのコントローラーに呼び出し可能な部分を追加する必要があることです。したがって、この時点ではルーターは正常に見えますが、たとえば新しいAboutControllerを追加する場合は、構成の先頭を次のように変更する必要があります...

return array(
'controllers' => array(
    'invokables' => array(
        'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',
        'Landlord\Controller\About' => 'Landlord\Controller\AboutController',
    ),
),
于 2012-11-30T18:34:53.970 に答える
3

答えを見つけてください...ここに投稿すると、誰かが役立つかもしれません... zf2 docsが提供するアルバムモジュールの例に従っていて、私の場合は「Admin」のように新しいモジュールを追加していると思います。 admin、newsなどの新しいコントローラー、

これがルーターの設定です...

'router' => array(
        'routes' => array(
            'admin' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/admin/admin[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Admin\Controller\Admin',
                        'action' => 'index',
                    ),
                ),

            ),
            'news' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/admin/news[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Admin\Controller\News',
                        'action' => 'index',
                    ),
                ),

            ),
        ),
    ),

そして、ここにコントローラーを追加します。

'controllers' => array(
        'invokables' => array(
            'Admin\Controller\News' => 'Admin\Controller\NewsController',
            'Admin\Controller\Admin' => 'Admin\Controller\AdminController',

        ),
    ),

これで、私の場合のように、モジュール内でコントローラーを呼び出すことができます

localhost/{foldername}/admin/news/index and localhost/{foldername}/admin/admin/index

ありがとう

于 2013-01-30T20:14:13.247 に答える
1

私も同じケースで、前の投稿と同じ解決策を見つけましたが、それは良い方法ではないと思います。

ルーター内にファイル「clientController」を追加するには、次のようにします。

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Fcm\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),
        'client' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/client[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Fcm\Controller\Client',
                    'action' => 'index',
                ),
            ),
        ),
    ),
),
于 2013-05-28T12:01:47.070 に答える
0

次のようにルーティングを構成します。-
モジュール:-ユーザー
コントローラー:-IndexController

'router' => array(
        'routes' => array(
            'users' => array(
                'type' => 'Literal',
                'options' => array(
                    // Change this to something specific toyour module
                    'route' => '/users',
                    'defaults' => array(
                                // Change this value to reflect the namespace in which
                        // the controllers for your module are found
                        '__NAMESPACE__' => 'Users\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' =>
                            '/[:controller[/:action][/:id]]',
                            'constraints' => array(
                                'controller' =>'[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' =>'[a-zA-Z][a-zA-Z0-9_-]*',
                                'id' =>'[0-9]+',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
于 2015-11-29T10:20:08.760 に答える