CakePHP自定义路由类 - 如何传递参数? [英] CakePHP Custom Route Classes - How to Pass Arguments?
问题描述
我创建了一个自定义路由类,我想能够传递设置/选项到构造函数,使其可配置。可以这样做吗?
自定义路由类的文档:
http://book.cakephp.org/2.0/en/development /routing.html#custom-route-classes
我的自定义路由类:
https://github.com/Signified/CakePHP-Model-Route -Class
您可以只传递路由器选项中可能有的任何设置/选项: :connect function。
App :: import('Lib','ModelRoute');
Router :: connect('/',array('controller'=>'pages','action'=>'display'),
Array('routeClass'=>'ModelRoute ',
'someMoreOptions'=>'OptionValue'));然后你可以在你的构造函数中检索一些关键字someMoreOptions
$ b b public function __construct($ settings = array())
{
$ this-> settings = Set :: merge($ this->设置,$设置);
//现在你可以使用传递的选项做一些事情。
if(isset($ this-> settings ['someMoreOptions'])
DoSomethingWith($ this-> settings ['someMoreOptions']);
}
I've created a custom route class and I want to be able to pass in settings/options to the constructor so that it's configurable. Can this be done?
Documentation for Custom Route Classes:
http://book.cakephp.org/2.0/en/development/routing.html#custom-route-classes
My custom route class:
https://github.com/Signified/CakePHP-Model-Route-Class
解决方案 You can probably just pass any settings/options you might have in the options of your Router::connect function.
App::import('Lib', 'ModelRoute');
Router::connect('/', array('controller' => 'pages', 'action' => 'display'),
Array('routeClass' => 'ModelRoute',
'someMoreOptions' => 'OptionValue' ));
Then you can retrieve the key someMoreOptions in your constructor
public function __construct($settings = array())
{
$this->settings = Set::merge($this->settings, $settings);
// Now you can do something with the option passed.
if(isset($this->settings['someMoreOptions'])
DoSomethingWith($this->settings['someMoreOptions']);
}
这篇关于CakePHP自定义路由类 - 如何传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!