如何在 Magento 控制器中获取 url 参数? [英] How to get a url parameter in Magento controller?

查看:21
本文介绍了如何在 Magento 控制器中获取 url 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个 Magento 函数可以从这个 url 中获取id"的值:

Is there a Magento function to get the value of "id" from this url:

http://example.com/path/action/id/123

我知道我可以在/"上拆分 url 来获取值,但我更喜欢单个函数.

I know I can split the url on "/" to get the value, but I'd prefer a single function.

这不起作用:

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

它仅在我使用 http://example.com/path/action?id=123 时有效

It only works if I use http://example.com/path/action?id=123

推荐答案

Magento 的默认路由算法使用三部分 URL.

Magento's default routing algorithm uses three part URLs.

http://example.com/front-name/controller-name/action-method

所以当你打电话

http://example.com/path/action/id/123

path 是你的前台名称,action 是你的控制器名称,id 是你的操作方法.这三个方法之后,就可以使用getParam来抓取键值对

The word path is your front name, action is your controller name, and id is your action method. After these three methods, you can use getParam to grab a key/value pair

http://example.com/path/action/id/foo/123

//in a controller
var_dump($this->getRequest()->getParam('foo'));

你也可以使用getParams方法来获取参数数组

You may also use the getParams method to grab an array of parameters

$this->getRequest()->getParams()

这篇关于如何在 Magento 控制器中获取 url 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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