CakePHP自定义路由类 - 如何传递参数? [英] CakePHP Custom Route Classes - How to Pass Arguments?

查看:112
本文介绍了CakePHP自定义路由类 - 如何传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义路由类,我想能够传递设置/选项到构造函数,使其可配置。可以这样做吗?



自定义路由类的文档



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屋!

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