CakePHP Custom Route Classes - How to Pass Arguments?

試著忘記壹切 提交于 2019-12-11 04:47:10

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/8089273/cakephp-custom-route-classes-how-to-pass-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!