OctoberCMS news list / detail page

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-02 17:43:47

问题


After programming plain HTML/CSS/Javascript/PHP I've just started using the CMS called OctoberCMS since both the Laravel framework and OctoberCMS look very well structured and easy to use/maintain. But I'm a little confused on how to process a single detail page or a overview page.

Let's take a news page for example. So far I've made this page:

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>

But where do I actually manage to make some sort of library for my news articles? I've read something about creating a new class in a new plug-in but I can't find any tutorials or documentation for this problem, or I'm just using the wrong terms while searching. Can someone make a small example (maybe with news articles) or post a link where I can find a tutorial/documentation?


回答1:


That is more comfortable to use plugin instead for write all code yourself.

Rain lab plugin allow create, manage, categorize, edit all kinds articles (include news).

You can get admin part from that plugin and use your visitor view.




回答2:


Documentation: https://octobercms.com/docs/plugin/registration

If you want to generate some code in command line here are some useful commands:

Generate plugin registration file and folders

php artisan create:plugin AuthorName.PluginName

Generate model

php artisan create:model AuthorName.PluginName ModelName

Generate controller

php artisan create:controller AuthorName.PluginName ModelNames

Refresh (reinstall) plugin

php artisan plugin:refresh AuthorName.PluginName

This should get you going, docs will be helpful after that.




回答3:


Use Builder (https://octobercms.com/plugin/rainlab-builder) plugin to manage CRUD very easily.

Suppose you have a model named NewsModel and you want to show the news listing or a single news in the frontend then you can modify your code by the following..

N.B: No need to write php opening and closing tag in the php section, just write

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>


来源:https://stackoverflow.com/questions/32685343/octobercms-news-list-detail-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!