【问题标题】:Can i create a url like google mail in cake php?我可以在 cake php 中创建像谷歌邮件这样的网址吗?
【发布时间】:2011-07-31 20:34:46
【问题描述】:

嗨,CakePHP 大师!我想问一些事情。

我觉得problem很简单,是否可以像这样在cakephp应用程序中生成一个url? (这是类似 url 的谷歌邮件 - http://mail.google.com/a/domain.com

http://www.domain.com/a/[something]/[member]/[controller]/[action]

其中 [something] 是动态变量,[member] 是前缀,[controller] 和 [action] 通常是 cakephp url 元素。

所以我想将通常的 CakePHP url 向右移动,或者在初始 url 中插入 /a/something/。 另外,应该从控制器动作中读取[something]

谢谢,您的帮助对我来说非常有价值,并且会真正帮助我

问题 v2: 我试图将此路由添加到 routes.php

Router::connect('/:code/:something/:controller/:action',
    array(),
    array('code'=>'a','pass'=>array('something'))
);

当我尝试访问http://domain.local/a/pte/users/view 时,它可以工作,但是当我尝试访问http://domain.local/a/pte/users 时它显示错误

问题 v3:

所以简单的问题是:

如何在不破坏 默认 CakePHP 路由?

QUESTION v4:(感谢@Rui)

我尝试创建此路由:

Router::connect('/:code/:something/:prefix/:controller/*',
    array(),
    array('code'=>'a','pass'=>array('something'))
);

我尝试访问时没问题

http://domain.local/a/pte/member/users(结果是 cake render member_index 视图,进步很大:-)),

但有几个问题:

  1. 当我尝试访问http://domain.local/a/pte/users没有前缀成员,我决定显示索引函数)失败(显示错误 AController 未定义)
  2. 当我创建一个链接时

    echo $html->link('test link',array('code'=>'a','something'=>'pte','member'=>true,'controller'=>'users' ,'action'=>'another_view'));

它会生成这样的链接

http://domain.local/member/users/another_view/code:a/something:pte

【问题讨论】:

    标签: cakephp routing


    【解决方案1】:

    请在 /app/config/routes.php 中尝试:

    <?php 
       Router::connect('/a/:something/:member/:controller/:action/*'); 
    ?>
    

    动作应该是:

    <?php 
        public function action($something = null, $member = null) { 
        /* action code */
        } 
    ?>
    

    希望对你有帮助。

    【讨论】:

    • 感谢您的回答.. 我已尝试编写此路由 Router::connect('/:code/:something/:controller/:action', array(), array('code' =>'a','pass'=>array('something')) );但这在我写 domain.com/a/test/users/view 时很有用,当我写 domain.com/a/test/users 时显示错误
    • 对于 domain.com/a/test/users,添加这个路由:Router::connect('/a/:something/:member/:controller', array('action' => '看法'));让我知道它是否有帮助,它假定视图是您在控制器中的默认操作。
    • 到目前为止它是伟大的瑞!但是当我尝试访问domain.local/a/pte/member/users 时,蛋糕告诉我错误:控制器UsersController 中未定义操作视图,不应该是member_view 而不仅仅是视图,因为我在核心中定义了前缀成员
    • 是的 cake 正在控制器用户中寻找动作“视图”...这就是为什么它说错误.....
    • 也许您正在寻找的是路由前缀...请在此处查看book.cakephp.org/view/950/Prefix-Routing,如果有帮助请告诉我。
    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2018-06-29
    相关资源
    最近更新 更多