josegonzalez/cakephp-sham

此包已被弃用且不再维护。没有建议的替代包。

一个用于处理SEO相关信息的CakePHP插件

安装: 28

依赖: 0

建议者: 0

安全: 0

星标: 15

关注者: 1

分支: 6

开放问题: 0

类型:cakephp-plugin

1.0.0 2014-04-13 10:36 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:34:08 UTC


README

Build Status Coverage Status Total Downloads Latest Stable Version Documentation Status Gratipay

需求

  • CakePHP 2.x

安装

[使用 Composer]

在Packagist上查看,并将最新版本的json片段复制到您的项目的composer.json文件中。例如,v. 1.0.0将看起来像这样

{
    "require": {
        "josegonzalez/cakephp-sham": "1.0.0"
    }
}

因为此插件在其自己的composer.json中设置了类型cakephp-plugin,所以composer知道将其安装在您的/Plugins目录中,而不是在常规的vendor文件中。建议您将/Plugins/Sham添加到您的.gitignore文件中。(为什么?阅读此内容。)

[手动]

[GIT 子模块]

在您的app目录中输入

git submodule add git://github.com/josegonzalez/cakephp-sham.git Plugin/Sham
git submodule init
git submodule update

[GIT Clone]

在您的插件目录中输入

git clone git://github.com/josegonzalez/cakephp-sham.git Sham

启用插件

在使用之前,您必须启用此插件

CakePlugin::load('Sham');

如果您在使用前已经使用过CakePlugin::loadAll();,则此步骤不是必需的。

使用

将组件和帮助器添加到您的AppController的$components$helpers数组中

<?php
class AppController extends Controller {
    public $components = array('Sham.Sham');
    public $helpers = array('Sham.Sham');
}
?>

默认情况下,Sham不会自动从数据库中加载SEO数据。您应该在控制器中创建回调来执行此操作。回调基于每个操作,以_seo开头,其中操作的第一个字母是大写

<?php
class UsersController extends AppController {

    public function profile($username = null) {
        // Some code that works with user profiles
    }

    public function _seoProfile() {
        // Called in the beforeRender() if the action was successfully processed
        $user = $this->viewVars['user'];
        $this->Sham->loadBySlug('view/' . $user['User']['username']);

        // Set some defaults in case the record could not be loaded from the DB
        $description = "awesome description of the page, with some good default keywords, referencing {$user['User']['username']}";
        $keywords = array($user['User']['username'] . ' profile', 'profiles', 'social network');

        $this->Sham->setMeta('title', "{$user['User']['username']}'s Profile  | Social Network");
        $this->Sham->setMeta('description', $description);
        $this->Sham->setMeta('keywords', implode(', ', $keywords));
        $this->Sham->setMeta('canonical', "/view/{$user['User']['username']}/", array('escape' => false));
    }
}
?>

如果您没有特定操作的回调,始终可以选择指定“回退”方法。这可以在组件设置中配置,但默认为Controller::_seoFallback()

<?php
class AppController extends Controller {
    public $components = array('Sham.Sham');
    public $helpers = array('Sham.Sham');

    public function _seoFallback() {
        // ... code ...
    }
}
?>

加载SEO数据后,就是为视图设置它的时候了。包含一个ShamHelper,它将自动处理这些细节

<!DOCTYPE html>
<html lang="en">
    <head>
        <?php echo $this->Sham->out('charset'); ?>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <?php echo $this->Sham->out(null, array('skip' => array('charset'))); ?>
        <?php echo $this->Html->css(array('style', 'uniform.default')); ?>
    <head>
    <body>
        ...
    </body>
</html>

如您所见,在必要时我们可以调用单个SEO信息 - 在这种情况下是字符集 - 然后通过将null作为选项传递给帮助器来调用其余部分。在某些情况下,这可能非常有用,您可能需要以特定的顺序具有SEO数据。

待办事项

  • 记录帮助器和组件选项

许可证

版权所有 (c) 2011 Jose Diaz-Gonzalez

特此授予任何获得此软件及其相关文档文件(“软件”)副本的任何人无限制使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向软件提供者提供软件的人这样做,但受以下条件约束

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“现状”提供,不提供任何形式的保证,无论是明示的、暗示的还是与商销性、适用于特定目的和非侵权性相关的保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论这些责任是因合同行为、侵权或其他行为而产生,无论这些责任是否与软件或软件的使用或其他交易有关。