如何为路由器中的密钥指定动态默认值Zend Framework

时间:2022-09-06 11:13:42

This question is linked to this one

这个问题与这个问题有关

How can I set the default of the category part to be the category value in the request url?

如何将类别部件的默认值设置为请求URL中的类别值?

$Router=$this->_front->getRouter();
$CategoryRoute = new Zend_Controller_Router_Route('category/:category/:controller/:action/*',
           array(
                 'controller' => 'index',
                 'action'     => 'index',
                 'category'   => 'aaa'
           ));
$Router->addRoute('category', $CategoryRoute);

In other words, I need the value [aaa] to be the value of category in the time I am building this route. There will always be a value for [category] as otherwise it will use the default route.

换句话说,我需要将值[aaa]作为构建此路线时类别的值。 [category]将始终存在值,否则将使用默认路由。

Example of what I mean:
If I surf to the site with url http://baseurl/category/mycat/index
I will be routed to controller=index, action=index, category=mycat.
But, in all my view files, where I use the Zend_View::url() helper, the links will point to:
http://baseurl/category/aaa/somthing/somthing (Using the exact route from above)
While I actually need them to point to:
http://baseurl/category/mycat/somthing/somthing

我的意思是:如果我使用url http:// baseurl / category / mycat / index浏览网站,我将被路由到controller = index,action = index,category = mycat。但是,在我使用Zend_View :: url()帮助器的所有视图文件中,链接将指向:http:// baseurl / category / aaa / somthing / somthing(使用上面的确切路径)我实际上需要他们指向:http:// baseurl / category / mycat / somthing / somthing

This happens because the default value for category is written as a constant in the route, and not taken, somehow, from the current URL.
I currently solve this by extracting by myself the category from the URL and making it the default.

发生这种情况是因为类别的默认值在路由中写为常量,而不是从当前URL以某种方式获取。我目前通过自己从URL中提取类别并将其作为默认值来解决此问题。

2 个解决方案

#1


Is there a question in your post? :)

你的帖子中有问题吗? :)

IF I got you right - yousetup your default category and it will be used if you call

如果我找对你 - 你设置了默认类别,如果你打电话,它将被使用

$this->url(array(),'category');

But when you use

但是当你使用的时候

$this->url(array('category'=>'my-category'),'category');

it will return category/my-category/index/idnex instead of category/aaa/index/index

它将返回category / my-category / index / idnex而不是category / aaa / index / index

#2


If you use

如果你使用

$this->url(array(), 'category'); 

it should work.

它应该工作。

But keep in mind : as long as you stick to the default values, you will always get a simple /category link, but when using different parameters it should return the correctly adapted URL based on the current URI.

但请记住:只要您坚持使用默认值,您将始终获得简单/类别链接,但在使用不同参数时,它应根据当前URI返回正确调整的URL。

ie. if URI is /category/aaa/index/index then $this->url(array(), 'category'); will return /category, if URI is /category/xxx/index/index then $this->url(array(), 'category'); will return /category/xxx, if URI is /category/xxx/index/process then $this->url(array(), 'category'); will return /category/xxx/index/process, etc.

即。如果URI是/ category / aaa / index / index则是$ this-> url(array(),'category');将返回/类别,如果URI是/ category / xxx / index / index则是$ this-> url(array(),'category');将返回/ category / xxx,如果URI是/ category / xxx / index / process则是$ this-> url(array(),'category');将返回/ category / xxx / index / process等。

#1


Is there a question in your post? :)

你的帖子中有问题吗? :)

IF I got you right - yousetup your default category and it will be used if you call

如果我找对你 - 你设置了默认类别,如果你打电话,它将被使用

$this->url(array(),'category');

But when you use

但是当你使用的时候

$this->url(array('category'=>'my-category'),'category');

it will return category/my-category/index/idnex instead of category/aaa/index/index

它将返回category / my-category / index / idnex而不是category / aaa / index / index

#2


If you use

如果你使用

$this->url(array(), 'category'); 

it should work.

它应该工作。

But keep in mind : as long as you stick to the default values, you will always get a simple /category link, but when using different parameters it should return the correctly adapted URL based on the current URI.

但请记住:只要您坚持使用默认值,您将始终获得简单/类别链接,但在使用不同参数时,它应根据当前URI返回正确调整的URL。

ie. if URI is /category/aaa/index/index then $this->url(array(), 'category'); will return /category, if URI is /category/xxx/index/index then $this->url(array(), 'category'); will return /category/xxx, if URI is /category/xxx/index/process then $this->url(array(), 'category'); will return /category/xxx/index/process, etc.

即。如果URI是/ category / aaa / index / index则是$ this-> url(array(),'category');将返回/类别,如果URI是/ category / xxx / index / index则是$ this-> url(array(),'category');将返回/ category / xxx,如果URI是/ category / xxx / index / process则是$ this-> url(array(),'category');将返回/ category / xxx / index / process等。