带 html 帮助器的 CakePHP Canonical 标签

发布于 2024-10-22 04:09:42 字数 213 浏览 7 评论 0原文

我如何使用 html 帮助器创建它? (使用 inline=false ,这样我就可以在每个视图的基础上指定它)

<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish" />

除了不起作用的补丁之外,似乎找不到任何相关内容。

How can I create this using the html helper? (with inline=false so i can specify it on a per-view basis)

<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish" />

Can't seem to find anything on this, apart from a patch that doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若沐 2024-10-29 04:09:42

在 CakePHP 错误跟踪网站中找到了这个: http://cakephp.lighthouseapp.com/projects/42648/tickets/1063-support-for-custom-meta-tag-elements-in-htmlhelper

显然你可以使用

echo $this->Html->meta('canonical', 'http:://example.com', array('rel'=>'canonical', 'type'=>null, 'title'=>null));
//outputs <link href="http:://example.com" rel="canonical" />

Found this in CakePHP bugtracking site : http://cakephp.lighthouseapp.com/projects/42648/tickets/1063-support-for-custom-meta-tag-elements-in-htmlhelper

Apparently you can use

echo $this->Html->meta('canonical', 'http:://example.com', array('rel'=>'canonical', 'type'=>null, 'title'=>null));
//outputs <link href="http:://example.com" rel="canonical" />
栖迟 2024-10-29 04:09:42

好像我的朋友刚刚告诉我,几个月前我告诉他如何做到这一点,问题解决了......

<?php echo $this->Html->meta('canonical', 
    'http://www.example.com/product.php?item=swedish-fish', 
    array('rel'=>'canonical', 'type'=>null, 'title'=>null, 'inline' => false)
);?>

It seems my friend just told me that I told him how to do this a few months back, problem solved...

<?php echo $this->Html->meta('canonical', 
    'http://www.example.com/product.php?item=swedish-fish', 
    array('rel'=>'canonical', 'type'=>null, 'title'=>null, 'inline' => false)
);?>
听,心雨的声音 2024-10-29 04:09:42

如果您正在寻找自动将当前 url 输出到规范标记中的东西,您可以使用 $this->Html->url(null, true);$ Cakephp html 帮助程序中的 this->here;

<?php echo $this->Html->meta('canonical', $this->Html->url(null, true), array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>

或者

<?php echo $this->Html->meta('canonical', $this->here, array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>

警告:
我听说过一些情况,其中 $this->here 在本地开发环境中存在问题。

If you're looking for something that automatically outputs the current url into a canonical tag, you can use the $this->Html->url(null, true); or $this->here; within the Cakephp html helper.

<?php echo $this->Html->meta('canonical', $this->Html->url(null, true), array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>

Or

<?php echo $this->Html->meta('canonical', $this->here, array('rel'=>'canonical', 'type'=>null, 'title'=>null)); ?>

WARNING:
I have heard of some cases where $this->here has issues on local dev environments.

拥抱没勇气 2024-10-29 04:09:42

在 CakePHP 2 中:

echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'inline' => false));

在 CakePHP 3 中:

echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'block' => true));

请注意,版本之间的主要区别是 CakePHP 2 使用 'inline' => 。 false 而 CakePHP 3 使用 'block' =>; true 将它们放置在文档 标记中。

In CakePHP 2:

echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'inline' => false));

In CakePHP 3:

echo $this->Html->meta('canonical', 'http://example.com', array('rel' => 'canonical', 'type' => null, 'title' => null, 'block' => true));

Note that the main difference between versions is that CakePHP 2 uses 'inline' => false whereas CakePHP 3 uses 'block' => true to place these within the document <head> tags.

孤寂小茶 2024-10-29 04:09:42

CakePHP 4 中:

在您的视图中(例如:Articles/view.php)添加以下内容:

<?php $this->Html->meta(
  'canonical',
  Router::url(['controller' => 'Articles', 'action' => 'view', $article->slug], true),
  [
    'block' => true
  ]
);
?>

然后使用以下指令将其打印在您的layout/default.ctp中

<?= $this->fetch('meta') ?>

In CakePHP 4:

In your view (es: Articles/view.php) add this:

<?php $this->Html->meta(
  'canonical',
  Router::url(['controller' => 'Articles', 'action' => 'view', $article->slug], true),
  [
    'block' => true
  ]
);
?>

Then you print it in your layout/default.ctp with this instruction

<?= $this->fetch('meta') ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文