0

我是 Zend 的新手,我对 Zend 路由器有点麻烦。我已经搜索过它,但没有找到...

我希望能够在 uri 级别为每个定义的变量定义一个路由器,以指向一个控制器中的不同操作。

我正在使用语言和模块,所以我在引导应用程序中定义了下一个 initRoutes 函数:

protected function _initRoutes()
{
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();

    $defaultRoute = new Zend_Controller_Router_Route(
        ':lang/:module/:controller/:action',
        array(
            'lang' => 'es',
            'module' => 'default',
            'controller' => 'index',
            'action' => 'index'
        ),
        array(
            'lang' => '^(en|es)$',
            'module' => '^(default|admin)$'
        )
    );

    $router->addRoute('defaultRoute', $defaultRoute);

    return $router;
}

我希望能够通过他们定义的操作访问论坛部分和论坛主题。

就像是 :

  • 我的域名/论坛 -> 论坛/索引

  • mydomain/forum/section -> 论坛/sectionAction

  • mydomain/forum/section/topic -> 论坛/topicAction

以及在 uri 级别定义的语言和模块,例如:

  • 我的域/语言/模块/论坛

  • mydomain/lang/module/forum/section

  • mydomain/lang/module/forum/section/topic

所以我有这个:

class ForumController extends Zend_Controller_Action
{

public function indexAction()
{
}

public function sectionAction()
{
}

public function topicAction()
{
}

然后我在 Default_Bootstrap 中创建了下一个路由:

$forumRoutes = new Zend_Controller_Router_Route(
    ':lang/:module/forum',
    array(
        'lang' => 'es',
        'module' => 'default',
        'controller' => 'forum',
        'action' => 'index'
    )
);

$sectionRoutes = new Zend_Controller_Router_Route(
    ':lang/:module/forum/:section',
    array(
        'lang' => 'es',
        'module' => 'default',
        'controller' => 'forum',
        'action' => 'section',
        'section' => ''
    )
);

$topic = new Zend_Controller_Router_Route(
    ':lang/:module/forum/:section/:topic',
    array(
        'lang' => 'es',
        'module' => 'default',
        'controller' => 'forum',
        'action' => 'topic',
        'section' => '',
        'topic' => ''
    )
);

$router->addRoute('forumTopics', $topic);
$router->addRoute('forumSections', $section);
$router->addRoute('forum', $forumRoutes);

现在,这仅在我在 uri 级别定义语言和模块时才有效,但如果我定义为 => mydomain/forum/section | 则不起作用 部分/主题。这也给我的导航->菜单带来了另一个问题。如果我在路由器定义中将“论坛”定义为静态变量,当我将鼠标悬停在 navigatoin.xml 中定义的任何标签上时,uri 级别对于每个标签都有相同的值。

我试图制作这样的链条:

    $forumRoutes = new Zend_Controller_Router_Route(
        ':lang/:module/forum',
        array(
            'lang' => 'es',
            'module' => 'default',
            'controller' => 'forum',
            'action' => 'index'
        )
    );

    $section = new Zend_Controller_Router_Route(
        ':section',
        array(
            'action' => 'section',
            'section' => ''
        )
    )

    $topic = new Zend_Controller_Router_Route(
        ':topic',
        array(
            'action' => 'topic',
            'topic' => ''
        )
    )

    $chainedRoute = new Zend_Controller_Router_Route_Chain();
    $chainedRoute->chain($topic)
                 ->chain($section)
                 ->chain($forumRoutes);
    $router->addRoute($chainedRoute);

但这并不像我预期的那样工作。

任何帮助将不胜感激,谢谢。

4

2 回答 2

2

你是 Zend 的新手。你说的。所以这里有一些解释:

  1. 理想情况下,Zend 应用程序上的 URL 是:

example.com/controller/action/param-name/:param-value

所以在这种情况下,如果在UsersController下有一个名为Edit的动作,它将是:

example.com/users/edit

如果添加操作,它将是:

example.com/users/add

当您将第一个参数指定为变量时,它将与控制器请求发生冲突。示例:如果您说控制器是用户,但第一个参数接受一个值并将其放入员工中,那么 example.com/employees 和 example.com/user 的请求都将指向员工控制器,即使用户控制器存在!这又是一个理论!

您可能想要做的是,让路由只接受动态值而不是路由!您不希望用户路由您的应用程序,而是将用户路由到应用程序的不同部分。

关于语言,您需要使用Zend_Locale它将检查 HTML 语言

<html lang="nl"> or <html lang = "en">

希望事情清楚!:)

于 2012-08-15T18:02:27.573 回答
1

Here's a quick example which should help you work with routes like that in ZF.

If you are using a default project structure like the one you get when starting a new project using Zend Tool go into the Application folder.

In your Bootstrap.php set up something like this:

protected function _initRouteBind() {
        // get the front controller and get the router
        $front  = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();
        // add each custom route like this giving them a descriptive name 
        $router->addRoute(
            'addTheDescriptiveRouteNameHere',
            new Zend_Controller_Router_Route(
                '/:controller/:id/:action/:somevar',
                array(
                    'module'     => 'default', 
                    'controller' => ':controller',
                    'action'     => ':action', 
                    'id'         => ':id',
                    'somevar'    => ':somevar'
                )
            )
        );   
}

My example above is just to illustrate how you would use a route where the controller, the action and a couple of parameters are set in the url.

The controller and action should be resolved without any additional work however to get the 'id' or 'somevar' value you can do this in your controller:

public function yourAction()
{
    // How you get the parameters to pass in to a function
    $id = $this->getRequest()->getParam('id');
    $somevar = $this->getRequest()->getParam('somevar');

    // Using the parameters
    $dataYouWant = $this->yourAmazingMethod($id);
    $somethingElse = $this->yourOtherAmazingMethod($somevar);

    // Assign results to the view
    $this->view->data = $dataYouWant;
    $this->view->something = $somethingElse;
}     

So while you will want to make sure your methods handle the parameters being passed in with care (after all it is user supplied info) this is the principle behind making use of the route parameter binding. You can of course do things like '/site' as the route and have it direct to a CMS module or controller, then another for '/site/:id' where 'id' is a page identifier.

Also just to say a nice alternative to:

$id = $this->getRequest()->getParam('id');
$somevar = $this->getRequest()->getParam('somevar');

which wasn't that nice itself anyway since it was assuming a parameter was going to be passed, using shorthand conditional statement in conjunction with action helpers is:

$id = ($this->_hasParam('id')) ? $this->_getParam('id') : null;
$somevar = ($this->_hasParam('somevar')) ? $this->_getParam('somevar') : null;

If you are not familiar with this, the

($this->_hasParam('id'))

is the conditional test which if true assigns the value of whatever is on the left of the ':' and which if false assigns the value of whatever is on the right of the ':'.

So if true the value assigned would be

$this->_getParam('id')

and null if false. Does that help? :-D

于 2012-08-15T21:29:33.227 回答