0

In meinem Moduls module.config.php, ich habe so etwas wie dies:ZF3: Wie zu bestimmten Controller/Aktion auf der Grundlage von Methode und Route?

namespace Application; 

return [ 
    //... 
    // myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method 
    'myroute1' => [ 
     'type' => Zend\Router\Http\Literal::class, 
     'options' => [ 
      'route' => '/index/foo', 
      'defaults' => [ 
       'controller' => Controller\IndexController::class, 
       'action'  => 'foo', 
      ], 
     ], 
    ], 

    // myroute2 will route to IndexController fooAction if the route is request method is GET but regardless of requested route 
    'myroute2' => [ 
     'type' => Zend\Router\Http\Method::class, 
     'options' => [ 
      'verb'  => 'get', 
      'defaults' => [ 
       'controller' => Controller\IndexController::class, 
       'action'  => 'foo', 
      ], 
     ], 
    ], 
    //... 
]; 

Was ich versuche zu erreichen: /index/foo angefordert

  • Wenn Route UND angefordert wird durch GET Methode, dann sollte es an weitergeleitet werden. IndexControllerfooAction
  • Wenn r out /index/foo angefordert wird, und wird durch POST Methode angefordert, dann sollte es zu Indexcontrollerbar Aktion geleitet wird (merkt es barAction ist hier nicht fooAction)

Wie zu erreichen Das?

Antwort

2

Ein Hinweis an mich und alle anderen suchen, als zusätzlicher Hinweis auf @ delboy1978uk Antwort.

Die Antwort, die ich war auf der Suche ist so etwas wie dieses:

  • GET /index/foo => Indexcontroller fooAction
  • POST /index/foo => Indexcontroller barAction

So ist der Code in module.config.php Datei sein kann wie folgt:

return [ 
    //... 
    'myroute1' => [// The parent route will match the route "/index/foo" 
     'type' => Zend\Router\Http\Literal::class, 
     'options' => [ 
      'route' => '/index/foo', 
      'defaults' => [ 
       'controller' => Controller\IndexController::class, 
       'action'  => 'foo', 
      ], 
     ], 
     'may_terminate' => false, 
     'child_routes' => [ 
      'myroute1get' => [// This child route will match GET request 
       'type' => Method::class, 
       'options' => [ 
        'verb' => 'get', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'foo' 
        ], 
       ], 
      ], 
      'myroute1post' => [// This child route will match POST request 
       'type' => Method::class, 
       'options' => [ 
        'verb' => 'post', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'bar' 
        ], 
       ], 
      ] 
     ], 
    ], 
    //... 
]; 
+0

Sie haben gerade meine Stunden gespeichert. Gruß! –

Verwandte Themen