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

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

问题描述

有一个Magento函数从这个网址获取id的值:



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



我知道我可以拆分/上的URL来获取值,但我更喜欢单个函数。



这不起作用:

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

只有使用 http://example.com/path/action?id=123

解决方案

Magento的默认路由算法使用三个部分网址。

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

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

单词 path 是您的前台名称, action 是您的控制器名称, id 您的操作方法。 之后这三种方法,您可以使用 getParam 来获取键/值对

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

//在控制器中
var_dump($ this-> ; getRequest() - > getParam('foo'));

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

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


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

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

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

This doesn't work:

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

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

解决方案

Magento's default routing algorithm uses three part URLs.

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

So when you call

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

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'));

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

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

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

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