dtzhfb4869
2017-04-11 12:08
浏览 27
已采纳

在Cakephp 3中找不到“路由器”类

I am upgrading my cakephp version from 2.6.7 to 3.3.4. The following code was properly working to refer the home url:

<?php echo Router::url('/', true); ?>

But this not working in cakephp 3.3.4 and it shows the following error message:

Error: Class 'Router' not found 

Which I missed? Thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我正在将cakephp版本从2.6.7升级到3.3.4。 以下代码正确地工作以引用主页:

 &lt;?php echo Router :: url('/',true);  ?&gt; 
   
 
 

但这在cakephp 3.3.4中无效,它显示以下错误消息:

 错误:未找到类'路由器'
   
 
 

我错过了什么? 提前致谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsjlqkbpn029473708 2017-04-11 15:34
    已采纳

    You need to make yourself comfortable with PHP namespaces. The Router class lives in the Cake\Routing namespace, hence you have to use either:

    \Cake\Routing\Router::url()
    

    or import the class:

    use Cake\Routing\Router;
    

    That being said, in your views you may want to use the Url helper instead (as mentioned by Jacek B Budzynski in the comments), in order to avoid the hard dependency on the Router class:

    $this->Url->build('/', true)
    

    See also

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题