添加,编辑,删除和取消后返回到依赖的CRUD

时间:2016-11-03 15:51:51

标签: zend-framework2

我的地区取决于各省,即每个省都有不同的地区。例如。, enter image description here

enter image description here

当我点击添加新区时,会打开以下页面: enter image description here

问题是点击取消或添加区域后:主页面来了,没有区域(必要时):

enter image description here

“编辑”和“删除”中的情况类似。 我尝试了一个解决方案:所选省份的ID应与区域ID一起传递(待编辑/删除)。如果是Add,它可以正常工作,因为只传递了一个id。但是在编辑/删除的情况下,它不起作用,因为省和区的ID不能同时传递

<a href="<?php echo $this->url('districts', 
                    array('action'=>'edit', 'id' => $districts->id));?>">Edit</a>  
<a href="<?php echo $this->url('districts',
                    array('action'=>'delete', 'id' => $districts->id));?>">Delete</a> 

module.config.php:

<?php 

namespace Admin;

return array(
'router' => array(
    'routes' => array( 

        'profile' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array(
                'route' => '/profile[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Admin\Controller\Profile',
                    'action' => 'index',
                ),
            ),
        ),

        'provinces' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array( 
                'route' => '/provinces[/:action][/:id]', 
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Admin\Controller\Provinces',
                    'action' => 'index',
                ),
            ),
        ),

        'districts' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array(
                'route' => '/districts[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Admin\Controller\Districts',
                    'action' => 'index',
                ),
            ),
        ),



        'home' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    '__NAMESPACE__' => 'Admin\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(
                        ),
                    ),
                ),
            ),
        ),

    ),
),
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'factories' => array(
        'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
        'navigation_admin' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'controllers' => array(
    'invokables' => array( 

        'Admin\Controller\Index' => 'Admin\Controller\IndexController',
        'Admin\Controller\Profile' => 'Admin\Controller\ProfileController',

        'Admin\Controller\Provinces' => 'Admin\Controller\ProvincesController',
        'Admin\Controller\Districts' => 'Admin\Controller\DistrictsController',

    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(


        'layout/layout_admin'           => __DIR__ . '/../view/layout/layout.phtml',
        'admin/index/index' => __DIR__ . '/../view/admin/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),

    /*
    'strategies' => array(
        'ViewJsonStrategy',
    ), 
    */
),

// Placeholder for console routes
'console' => array(
    'router' => array(
        'routes' => array(
        ),
    ),
),

'navigation_admin' => array(
    'default' => array(
        array(
            'label' => 'Home',
            'route' => 'home',
        ),

        array(
            'label' => 'Profile',
            'route' => 'profile',
            'pages' => array( 
                array(
                    'label' => 'Edit',
                    'route' => 'profile',
                    'action' => 'edit',
                ), 
                array(
                    'label' => 'Change Password',
                    'route' => 'profile',
                    'action' => 'changepassword',
                ), 
            ),
        ),

        array(
            'label' => 'Provinces',
            'route' => 'provinces',
            'pages' => array(
                array(
                    'label' => 'Add',
                    'route' => 'provinces',
                    'action' => 'add',
                ),
                array(
                    'label' => 'Edit',
                    'route' => 'provinces',
                    'action' => 'edit',
                ),
                array(
                    'label' => 'Delete',
                    'route' => 'provinces',
                    'action' => 'delete',
                ),
            ),

        ),


        array(
            'label' => 'Districts',
            'route' => 'districts',

            'pages' => array(
                array(
                    'label' => 'Add',
                    'route' => 'districts',
                    'action' => 'add',
                ),
                array(
                    'label' => 'Edit',
                    'route' => 'districts',
                    'action' => 'edit',
                ),
                array(
                    'label' => 'Delete',
                    'route' => 'districts',
                    'action' => 'delete',
                ),
            ),

        ),



    ),  
),
);

出现这个问题是因为在这种情况下,这些地区依赖于省。没有依赖省的每件事情都可以。

那么我怎么能解决这个问题呢?请给我一些指导或建议,以便我继续工作。

1 个答案:

答案 0 :(得分:1)

'地区'路线只设置为'id',请将配置更改为以下。

        'districts' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array(
                'route' => '/districts[/:action][/:id][/province_id/:province_id]',
//or
//'route' => '/districts[/:action][/:id][/:province_id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Admin\Controller\Districts',
                    'action' => 'index',
                ),
            ),
        ),

并将网址更改为

<a href="<?php echo $this->url('districts', 
                    array('action'=>'edit', 'id' => $districts->id,'province_id'=>$provinceId));?>">Edit</a> 

你现在应该在'action'中有'province_id'参数来操作'view'。