php - 我们如何在 cakephp 的 Controller 名称前添加一些文本?

标签 php cakephp seo

我有一个 SEO 要求,当用户登录时,他们的用户名应该显示在 Controller 名称之前。

  • example.com/home - 目前看起来像这样
  • example.com/username/home - 我希望它看起来像这样

我已经在 routes.php 中尝试了一些基本的修改,例如:

Router::connect('/haresh/:controller', array('action' => 'index'));

有了这个,它就可以工作了,但是这个选项丢失了基本路径,所以所有的图像都是 消失。 IE。图片网址如下:

<img src='img/xyz.png' />

不工作。

如何修改url,让/username/成为所有url的前缀,并解决图片路径问题?

最佳答案

SEO适用于匿名用户

如果问题中的网址仅适用于已登录的用户 - 它们的 SEO 值(value)为零 - 因为 googlebot et.al。永远不会登录到您的网站,如果他们登录了 - 所有网址都将是 /googlebots-username/...

搜索引擎优化路线

假设这些用户 url 是可公开访问的(参见上一点,如果不是,它们就没有值(value)),您需要做的就是为它们定义一个路由:

// define other routes first
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));

Router::connect('/:username/:controller', array('controller' => 'default', 'action' => 'index'));
Router::connect('/:username/:controller/:action', array('controller' => 'default', 'action' => 'index'));
Router::connect('/:username/:controller/:action/*', array('controller' => 'default', 'action' => 'index'));

然后您可以访问 Controller 中的用户名属性作为

$username = $this->request->params['named']['username'];

修复图像

这是一个单独的问题,但简单的解决方法是使用 appropriate helper method :

<?php echo $this->Html->image('xyz.png', array('alt' => 'to the abc')); ?>

大多数标记问题的答案是使用适当的辅助方法。

或者

或者只使用绝对 url:

<img src='/img/xyz.png' />

显然,如果 url 发生变化 - 使用 Assets 的相对路径意味着请求的图像会根据当前 url 发生变化。

关于php - 我们如何在 cakephp 的 Controller 名称前添加一些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14534991/

相关文章:

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - MySQL UPDATE 或 INSERT 取决于 ID 的存在

php - CakePHP Complex 查找/查询 - 改进当前解决方案

wordpress - SEO - 使用 href ="#"的下拉菜单

php - $_SERVER ['REQUEST_METHOD' ] 是否仍然可行?

PHP - 字符串到货币的转换

mysql - 对使用存储过程的 CakePHP 应用程序进行单元测试

php - 序列化 POST 数组,以便 Cake PHP 可以插入 MySQL 数据库

xml - 如何增加站点地图索引的数量

html - 应该在哪里设置网站的语言?