动作参数路由在Zend框架route.ini中不起作用 [英] action parameters routing not working in Zend framework routes.ini

查看:118
本文介绍了动作参数路由在Zend框架route.ini中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Zend Framework(版本1.11.11)中的route.ini文件中设置一条路由,以允许其匹配以下网址:

I'm trying to set up a route in Zend Framework (version 1.11.11) in a routes.ini file, which would allow be to match the following url:

my.domain.com/shop/add/123

ShopController addAction 。但是,由于某种原因,我的操作无法识别参数(末尾的数字)。我得到的PHP错误是

to the ShopController and addAction. However, for some reason the parameter (the number at the end) is not being recognized by my action. The PHP error I'm getting is

Warning: Missing argument 1 for ShopController::addAction(), called in...

我知道我可以在引导程序中使用PHP代码进行设置,但我想了解如何在.ini文件中进行这种类型的设置,而我很难找到任何可以解释这一点的资源。我还应该指出,我在项目中使用模块。我使用在网上和网上找到的各种摘要得出的内容如下:

I know I could set this up using PHP code in the bootstrap, but I want to understand how to do this type of setup in a .ini file and I'm having a hard time finding any resources that explain this. I should also point out that I'm using modules in my project. What I've come up with using various snippets found here and there online is the following:

application / config / routes.ini:

application/config/routes.ini:

[routes]
routes.shop.route = "shop/add/:productid/*"
routes.shop.defaults.controller = shop
routes.shop.defaults.action = add
routes.shop.defaults.productid = 0
routes.shop.reqs.productid = \d+

Bootstrap.php:

Bootstrap.php:

... 
protected function _initRoutes() 
    {
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'routes');
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $router->addConfig( $config, 'routes' );
    }
...

ShopController.php

ShopController.php

<?php

class ShopController extends Egil_Controllers_BaseController
{

    public function indexAction()
    {
        // action body
    }

    public function addAction($id)
    {
        echo "the id: ".$id;
    }

}

关于这是为什么的任何建议不工作吗?我感觉我缺少通过.ini文件在Zend中进行路由的基本知识。

Any suggestions as to why this is not working? I have a feeling I'm missing something fundamental about routing in Zend through .ini files.

推荐答案

显然,我更生锈了在Zend中比我想象的要好。发布几分钟后,我意识到我试图以错误的方式在控制器中访问参数。它不应该是addAction的参数,而是我应该通过函数内的请求对象访问它:

Apparently I'm more rusty in Zend than I thought. A few minutes after posting I realized I'm trying to access the parameter the wrong way in my controller. It should not be a parameter to addAction, instead I should access it through the request object inside the function:

在ShopController中正确的addAction:

correct addAction in ShopController:

public function addAction()
{
    $id = $this->_request->getParam('productid');
    echo "the id: ".$id;
}

在这种情况下,我也意识到我可以简化路线设置:

I also realized I can simplify my route setup quite a bit in this case:

[routes]
routes.shop.route = "shop/:action/:productid"
routes.shop.defaults.controller = shop
routes.shop.defaults.action = index

这篇关于动作参数路由在Zend框架route.ini中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆