sip/news-bundle

简单的新闻套餐,具有sonata后端

安装: 23

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

公开问题: 0

类型:symfony-bundle

dev-master 2013-05-18 20:45 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:11:10 UTC


README

该套餐实现了网站上的新闻,还可以在首页分享新闻网站和新闻(使用mayn上的管理员中的复选框),支持orm和mongodb

安装

  1. 添加套餐到composer.json并下载包的命令。
$ composer require "sip/news-bundle": "dev-master"
  1. 在内核中启用套餐。
<?php

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        new SIP\NewsBundle\SIPNewsBundle(),
        new Genemu\Bundle\FormBundle\GenemuFormBundle(),
        // If you wish to use SonataAdmin
        new Sonata\BlockBundle\SonataBlockBundle(),
        new Sonata\jQueryBundle\SonatajQueryBundle(),
        new Sonata\AdminBundle\SonataAdminBundle(),

        // Other bundles...
    );
}

阅读有关安装SonataAdminBundle的更多信息

  1. 创建您的实体/文档

对于orm

<?php
namespace MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SIP\NewsBundle\Entity\News as BaseNews;

/**
 * @ORM\Entity
 * @ORM\Table(name="content_news")
 */
class News extends BaseNews
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}

对于mongodb

<?php
namespace MyBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use SIP\NewsBundle\Document\News as BaseNews;

/**
 * @MongoDB\Document(collection="content_news")
 */
class News extends BaseNews
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}
  1. 更新数据库模式(仅限orm)
$ php app/console doctrine:schema:update --force

这应在开发环境中完成!我们建议使用Doctrine迁移(仅限orm),以安全地更新您的模式。

  1. 导入路由配置
SIPNewsBundle:
    resource: '@SIPNewsBundle/Resources/config/routing.yml'
    prefix:   /news
  1. 配置

对于orm

# app/config/config.yml
sip_news:
    model: MyBundle\Entity\News
    # All Default configuration:
    # controller: SIP\\NewsBundle\\Controller\\NewsController
    # manager_type: orm
    # repository: Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
    # admin: SIP\NewsBundle\Admin\NewsAdmin

对于mongodb

# app/config/config.yml
sip_news:
    model: MyBundle\Document\SIP\News
    # controller: SIP\\NewsBundle\\Controller\\NewsController
    manager_type: mongodb
    repository: SIP\ResourceBundle\Repository\ODM\MongoDB\DocumentRepository
    # admin: SIP\NewsBundle\Admin\NewsAdmin
  1. 模板

该套餐需要show.html和list.html模板。覆盖视图的最简单方法是将它们放置在这里 app/Resources/SIPNewsBundle/views/News/index.html.twig app/Resources/SIPNewsBundle/views/News/item.html.twig app/Resources/SIPNewsBundle/views/News/main_index.html.twig。

  1. 使用方法

在主页显示新闻

控制器

<?php

namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MainController extends Controller
{
    /**
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function indexAction()
    {
        $recentNews = $this
            ->getNewsRepository()
            ->findBy(array('onMain' => 1), array('date' => 'desc'), 8);

        return $this->render('MyBundle:Main:index.html.twig',
            array('recentNews'     => $recentNews));
    }
}

模板(index.html.twig)

...
<div class="fluid-row" style="overflow: hidden;">
    {% for news in recentNews %}

    <div class="span3 well well-small">
        <a href="{{ path('sip_news_news_item', {'slug': news.slug})}}" class="thumbnail" style="width: 130px; height: 75px;">
            {% media news.image, 'normal' %}
        </a>
        <hr />

        <h4>
            <a href="{{ path('sip_news_news_item', {'slug': news.slug})}}">
                {{ news.title|slice(0, 20) }}...
            </a>
        </h4>

        <p>{{ news.description|slice(0, 50)|raw }}...</p>
        <p><a href="{{ path('sip_news_news_item', {'slug': news.slug})}}" class="btn pull-right">Read more</a>
    </div>

    {% if loop.index % 4 == 0 %}
</div>
<div class="fluid-row">
    {% endif %}

    {% endfor %}
</div>
...