定制 lexxpavlov/pagebundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lexxpavlov/pagebundle

Composer 安装命令:

composer require lexxpavlov/pagebundle

包简介

Symfony2 Page bundle with meta data, predefined form type and Sonata Admin service

README 文档

README

This bundle helps you to manage your static pages in Symfony2 project.

The bundle has a page entity with fields:

  • title - the title of page
  • content - html content. May use ckeditor for easy wysiwyg edit of content
  • slug - use as url of page. May be autogenerated based on title
  • published - enable or disable page
  • publishedAt, createdAt, updatedAt - Datetime fields, that contain actual information about page
  • meta: keywords and description - SEO info

If you use SonataAdminBundle, this bundle automatically adds an entity to it.

Installation

Composer

Download LexxpavlovPageBundle and its dependencies to the vendor directory. The bundle has a StofDoctrineExtensionsBundle as required dependency and IvoryCKEditorBundle as optional dependency.

You can use Composer for the automated process:

$ php composer.phar require lexxpavlov/pagebundle

or manually add link to bundle into your composer.json and run $ php composer.phar update:

{
    "require" : {
        "lexxpavlov/pagebundle": "~1.0"
    },
}

Composer will install bundle to vendor/lexxpavlov directory. Bundle StofDoctrineExtensionsBundle will be installed automatically, if it didn't install earlier.

Adding bundle to your application kernel

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
        new Lexxpavlov\PageBundle\LexxpavlovPageBundle(),
        // ...
    );
}

If you are already have StofDoctrineExtensionsBundle in the your AppKernel, you don't need to add its twice.

Configuration

First you must create your own page entity class. It's easy to make by extend base page from bundle.

<?php

namespace App\YourBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Lexxpavlov\PageBundle\Entity\Page as BasePage;

/**
 * @ORM\Entity()
 */
class Page extends BasePage
{
    // Be free to add your fields here
}

Here is the default configuration for the bundle:

stof_doctrine_extensions:
    default_locale: %locale%
    orm:
        default:
            timestampable: true

lexxpavlov_page:
    entity_class: App\YourBundle\Entity\Page

This will activate doctrine Timestampable extension. Also you may activate Sluggable and Blameable extensions (see below). See more about doctrine extensions at documentation.

Now you need create the table in your database:

$ php app/console doctrine:schema:update --dump-sql

This will show SQL query for creating the table in the database. You may manually run this query.

Note. You may also execute php app/console doctrine:schema:update --force command, and Doctrine will create needed table for you. But I strongly recommend you to execute --dump-sql first and check SQL, which Doctrine will execute.

Usage

If you use SonataAdminBundle, then you are already have admin tool for creating new pages. Otherwise you need to write your own creating tool, and here you may use predefined form:

$form = $this->createForm('lexxpavlov_page');

There is the sample code for showing page, controller class and twig template. There are 3 different versions of action code, that doing the same - get page from database and show it in the twig template. Choose one or write your code.

Controller:

{# src/App/YourBundle/Controller/DefaultController.php #}

namespace App\YourBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use App\YourBundle\Entity\Page;

class DefaultController extends Controller
{
    /**
     * @Route("/page/{id}.html")
     * @Template()
     */
    public function pageAction(Page $page)
    {
    }
    
    // or find by slug:
    
    /**
     * @Route("/page/{slug}")
     * @Template("AppYourBundle:Default:page.html.twig")
     */
    public function slugAction(Page $page)
    {
    }
    
    // or find from repository
    
    /**
     * @Route("/page-find/{id}")
     * @Template("AppYourBundle:Default:page.html.twig")
     */
    public function findAction($id)
    {
        $repository = $this->getDoctrine()->getRepository('AppYourBundle:Page');
        
        if (is_numeric($id)) {
            $page = $repository->find($id);
        } else {
            $page = $repository->findOneBySlug($id);
        }
        
        return array('page' => $page);
    }
}

And template:

{# src/App/YourBundle/Resources/views/Default/page.html.twig #}

{% extends '::layout.html.twig'%}

{% block meta %}
{% if page.metaKeywords is defined %}
    <meta name="Keywords" content="{{ page.metaKeywords }}">
{% endif %}
{% if page.metaDescription is defined %}
    <meta name="Description" content="{{ page.metaDescription }}">
{% endif %}
{% endblock %}

{% block body %}

<div class="page">
    <h1 class="page-title">{{ page.title }}</h1>
    <div class="page-info">Created at {{ page.createdAt|date('d.m.Y') }} by {{ page.createdBy.username }}</div>
    <div class="page-content">{{ page.content|raw }}</div>
</div>

{% endblock %}

Note. Do not forget add a meta block to the <head> section of layout.html.twig.

The page with id=1 and slug=test will be shown by controller at these urls:

  • /page/1.html
  • /page/test
  • /page-find/1
  • /page-find/test

Advanced configuration

Full configuration

lexxpavlov_page:
    entity_class: App\SiteBundle\Entity\Page
    admin_class: Lexxpavlov\PageBundle\Admin\PageAdmin # or false to disable registering of sonata admin service
    content_type: ckeditor # use your form type for content field, e.g. textarea or ckeditor

ckeditor form type is added by IvoryCKEditorBundle.

Activate autogeneration of slug field

LexxpavlovPageBundle marks slug field as @Gedmo\Slug. You need to activate its listener in StofDoctrineExtensionsBundle config:

stof_doctrine_extensions:
    # ...
    orm:
        default:
            sluggable: true
            # ...

StofDoctrineExtensionsBundle has a tool for build slug from any local string to latin-only string (urlizer). Urlizer gets any UTF-8 string, urlizes it and saves to slug field. For automatic filling you must left slug field blank while create or update the page. If slug field isn't blank, than Sluggable doesn't work.

Unfortunately, this automatic tool produce not perfect result, and you may want to write your own urlizer for your language and set up Sluggable extension to use that urlizer. You may see extension documentation and code of listener in this bundle.

This bundle has sample urlizer for Russian language:

stof_doctrine_extensions:
    class:
        sluggable: Lexxpavlov\PageBundle\Listener\RuSluggableListener
    # ...

Append autoupdating user fields

You may add createdBy and updatedBy fields to your entity and use Blameable doctrine extension. Make next changes to your page entity class:

<?php

namespace App\YourBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Gedmo\Mapping\Annotation as Gedmo;
use Lexxpavlov\PageBundle\Entity\Page as BasePage;

use App\YourBundle\Entity\User;

/**
 * @ORM\Entity()
 */
class Page extends BasePage
{
    
    /**
     * @var User
     *
     * @Gedmo\Blameable(on="create")
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
     */
    protected $createdBy;

    /**
     * @var User
     *
     * @Gedmo\Blameable(on="update")
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
     */
    protected $updatedBy;

    
    /**
     * Set user, that updated entity
     *
     * @param User $updatedBy
     * @return Page
     */
    public function setUpdatedBy($updatedBy)
    {
        $this->updatedBy = $updatedBy;

        return $this;
    }

    /**
     * Get user, that updated entity
     *
     * @return User 
     */
    public function getUpdatedBy()
    {
        return $this->updatedBy;
    }
    
    /**
     * Set user, that created entity
     *
     * @param User $createdBy
     * @return Page
     */
    public function setCreatedBy($createdBy)
    {
        $this->createdby = $createdBy;

        return $this;
    }

    /**
     * Get user, that created entity
     *
     * @return User 
     */
    public function getCreatedBy()
    {
        return $this->createdBy;
    }

}

And activate Blameable extension in StofDoctrineExtensionsBundle config:

stof_doctrine_extensions:
    # ...
    orm:
        default:
            blameable: true
            # ...

lexxpavlov/pagebundle 适用场景与选型建议

lexxpavlov/pagebundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 761 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「page」 「page bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lexxpavlov/pagebundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lexxpavlov/pagebundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 lexxpavlov/pagebundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 761
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-19