dty98339 2013-07-17 01:11
浏览 43
已采纳

CakePHP如何将参数传递给控制器​​?

In CakePHP I am trying to pass a simple parameter to my controller class method. However it looks like the parameter has to be visible in the URL. Can't I pass a parameter without it being visible in the URL?

My routing:

Router::connect(
    '/',
    array(
        'controller' => 'Pages',
        'action' => 'display'
    ),
    array(
        'pass' => array(
            'pageName' =>'home'
        )
    )
);

And my Controller method:

public function display($p_sPageName=null) {
  • 写回答

1条回答 默认 最新

  • dovzrfr0506 2013-07-17 01:17
    关注
    Router::connect(
        '/',
        array(
            'controller' => 'Pages',
            'action' => 'display',
            'home',
        ),
    );
    

    This should be a default route in a baked application and already present. The book has also a very good section explaining the routing.

    Also follow the CakePHP coding standard, this variable name $p_sPageName is bad. Nobody ever knows what $p_s means. This is a very good read about writing clean and readable code.

    /**
     * Displays a static page
     *
     * @param string $pageName
     * @return void
     */
        public function display($pageName = null) { /*...*/ }
    

    The doc block should tell you by "@param string $pageName" that it is a string not the variable name. Without documentation this becomes unreadable for everyone who does not know the naming conventions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部