donglefu6195 2013-08-15 06:25
浏览 7
已采纳

cakephp不会在url中显示索引操作

I'm using the following code in my AppHelper.php to inject the language parameter into links created with the HtmlHelper

public function url($url = null, $full = false) {
  if(!isset($url['language']) && isset($this->params['language'])) {
    $url['language'] = $this->params['language'];
  }
  return parent::url($url, $full);
}

It's working fine but when I go to the index action the url becomes something.com/some_controller/index

if I don't override the url method then the url is just something.com/some_controller

These are my routes

Router::connect('/:language/:controller/:action/*',
                   array(),
                   array('language' => 'eng|fra'));

Router::connect('/:language/:controller',
                   array('action' => 'index'),
                   array('language' => 'eng|fra')); 

Router::connect('/:language',
                   array('controller' => 'pages', 'action' => 'display'),
                   array('language' => 'eng|fra'));

I'm calling the link method like this

$this->Html->link(__('Users'), array('controller'=>'users', 'action'=>'index'))';

and I tried it without the action parameter

$this->Html->link(__('Users'), array('controller'=>'users'))';

without the action parameter but it adds the current action if I am on a page managed by the same controller

how can I make it so that the name of the action doesn't become part of the url if it is the index action?

  • 写回答

1条回答 默认 最新

  • duanpingzu7194 2013-08-18 08:13
    关注

    The routes are matched in the order in which you set them. Reverse the order of your Router::connect() statements and you are done. ie. First set route for /:language' then for /:language/:controller' and then /:language/:controller/:action:'

    Also overriding AppHelper::url() to include the language in generate url is not needed. Use the 'persist' key in your Router::connect() statements and it will do the job for you.

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

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答