带有URL映射的Zend Framework问题

时间:2022-02-02 20:14:16

I'm trying to map an action to the base URL and default controller.

我正在尝试将操作映射到基本URL和默认控制器。

Swear this should be straight forward or even an 'out the box' feature but how do you map actions in your default controller to the base url using the Zend Framework? I'm fairly new to the Framework so I'm hoping I'm just missing something obvious.

发誓这应该是直接的,甚至是“开箱即用”功能,但是如何使用Zend Framework将默认控制器中的操作映射到基本URL?我对框架很新,所以我希望我只是遗漏了一些明显的东西。

Trying to map:

试图映射:

domain.com/index/my-page to domain.com/my-page

domain.com/index/my-page到domain.com/my-page

without breaking any of the other routes setup by default.

默认情况下不会破坏任何其他路由设置。

I can hack it using the Zend_Router but it breaks the other rules e.g. /:controller/:action /:module/:controller/:action

我可以使用Zend_Router破解它,但它打破了其他规则,例如/:controller /:action /:module /:controller /:action

note: /index/my-page is an example url - I need it to work for all the indexController actions dynamically.

注意:/ index / my-page是一个示例url - 我需要它动态地为所有indexController动作工作。

URL Mapping examples as requested by 'tharkun' indexController has methods indexAction and contactAction need urls

'tharkun'indexController请求的URL映射示例有方法indexAction和contactAction需要urls

/index
/index/contact
/
/contact

2nd controller testController has methods indexAction and monkeyAction need urls

第二个控制器testController有方法indexAction和monkeyAction需要urls

/test
/test/index
/test/monkey

basically - if the sys can't find a controller of VAR then it looks for a action of VAR in the default controller

基本上 - 如果sys找不到VAR的控制器,那么它会在默认控制器中查找VAR的动作

1 个解决方案

#1


The default controller is IndexController.

默认控制器是IndexController。

The (default) mapping works like this:

(默认)映射的工作方式如下:

/ => IndexController::indexAction
/index => IndexController::indexAction
/index/foo => indexController::fooAction
/foo => FooController::indexAction

So, add a user defined route like this (will have lower priority than the default)

因此,添加这样的用户定义路由(优先级低于默认值)

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
    '/:action',
    array(
        'controller' => 'index'
    )
);
$router->addRoute('user', $route);

This did not break my use of the default routes.

这并没有打破我对默认路由的使用。

Edit: As coffeerings said in the comment, this will break the indexAction of non-default controllers.

编辑:正如coffeerings在评论中所说,这将打破非默认控制器的indexAction。

#1


The default controller is IndexController.

默认控制器是IndexController。

The (default) mapping works like this:

(默认)映射的工作方式如下:

/ => IndexController::indexAction
/index => IndexController::indexAction
/index/foo => indexController::fooAction
/foo => FooController::indexAction

So, add a user defined route like this (will have lower priority than the default)

因此,添加这样的用户定义路由(优先级低于默认值)

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
    '/:action',
    array(
        'controller' => 'index'
    )
);
$router->addRoute('user', $route);

This did not break my use of the default routes.

这并没有打破我对默认路由的使用。

Edit: As coffeerings said in the comment, this will break the indexAction of non-default controllers.

编辑:正如coffeerings在评论中所说,这将打破非默认控制器的indexAction。