【问题标题】:OctoberCMS news list / detail page十月CMS新闻列表/详情页
【发布时间】:2015-12-17 13:40:57
【问题描述】:

在编写了普通的 HTML/CSS/Javascript/PHP 之后,我刚刚开始使用名为 OctoberCMS 的 CMS,因为 Laravel 框架和 OctoberCMS 看起来结构非常好并且易​​于使用/维护。但是我对如何处理单个详细信息页面或概览页面有点困惑。

我们以新闻页面为例。到目前为止,我已经制作了这个页面:

title = "News"
url = "/news/:news_id?|^[0-9]+$"
layout = "default"
description = "This is the news page."
is_hidden = "0"
meta_title = "News"
meta_description = "News page meta description"
==
<?php
function onStart()
{
    $news_id = $this->param('news_id');
    if(isset($news_id)) {
        $news_article = []; //get the news article by id
        $this['news_article'] = $news_article;
    } else {
        $news = []; //get an array of news articles (last ... articles ordered by datetime desc)
        $this['news'] = $news;
    }
}
?>
==
<div class="container">
    <h1 class="block-title">
        News
    </h1>
    {% if news_article is defined %}
        Article
    {% else %}
        Overview
    {% endif %}
</div>

但是我实际上在哪里设法为我的新闻文章建立某种图书馆?我已经阅读了有关在新插件中创建新类的内容,但找不到任何有关此问题的教程或文档,或者我只是在搜索时使用了错误的术语。有人可以做一个小例子(可能是新闻文章)或发布一个链接,我可以在其中找到教程/文档吗?

【问题讨论】:

    标签: php octobercms


    【解决方案1】:

    使用插件代替自己编写所有代码会更舒服。

    Rain lab plugin 允许创建、管理、分类、编辑各种文章(包括新闻)。

    您可以从该插件获取管理部分并使用您的访问者视图。

    【讨论】:

      【解决方案2】:

      文档:https://octobercms.com/docs/plugin/registration

      如果你想在命令行中生成一些代码,这里有一些有用的命令:

      生成插件注册文件和文件夹

      php artisan create:plugin AuthorName.PluginName
      

      生成模型

      php artisan create:model AuthorName.PluginName ModelName
      

      生成控制器

      php artisan create:controller AuthorName.PluginName ModelNames
      

      刷新(重新安装)插件

      php artisan plugin:refresh AuthorName.PluginName
      

      这应该可以帮助您,之后文档会很有帮助。

      【讨论】:

        【解决方案3】:

        使用 Builder (https://octobercms.com/plugin/rainlab-builder) 插件非常容易地管理 CRUD。

        假设您有一个名为 NewsModel 的模型,并且您想在前端显示新闻列表或单个新闻,那么您可以通过以下方式修改您的代码..

        N.B:不需要在php部分写php开始和结束标签,直接写

        use Namespace\Plugin\Models\NewsModel; //needed to get data through model
        function onStart()
        {
            $news_id = $this->param('news_id');
            if($news_id) {
                $news_article = []; //get the news article by id
                $this['news_article'] = $news_article;
            } else {
                $news = []; //get an array of news articles (last ... articles ordered by datetime desc)
                $this['news_list'] = $news;
            }
        }
        ==
        <div class="container">    
            {% if news_article %}
                <h1 class="block-title"> News Details</h1>
                <div>{{ news_article.details }}</div> <!--Suppose you have a field named 'details' in the news table -->
            {% elseif news_list %}
                <h1 class="block-title"> News List</h1>
                <ul>
                {% for news in news_list %}
                   <li> {{ news.title }}</li><!--Suppose you have a field named 'title' in the news table -->
                {% endfor %}
                </ul>
            {% else %}
                No news found !
            {% endif %}
        </div>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-18
          • 1970-01-01
          • 1970-01-01
          • 2022-01-13
          • 1970-01-01
          • 2015-06-07
          • 2019-02-03
          相关资源
          最近更新 更多