在编写了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>
但是我在哪里可以为我的新闻文章建立一个库呢?我读过一些关于在新插件中创建新类的内容,但我找不到任何关于此问题的教程或文档,或者我只是在搜索时使用了错误的术语。有人可以做一个小的例子(也许是新闻文章)或张贴一个链接,我可以找到一个教程/文档?
使用插件代替自己编写所有代码更舒适。
Rain lab插件允许创建,管理,分类,编辑各种文章(包括新闻)。
您可以从该插件中获得管理部分,并使用您的访问者视图
文档: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
这应该会让你开始,文档之后会有帮助。
使用Builder (https://octobercms.com/plugin/rainlab-builder)插件非常容易地管理CRUD。
假设您有一个名为NewsModel的模型,并且您希望在前端显示新闻列表或单个新闻,那么您可以通过以下方式修改代码…
N。B:不需要在php部分写php的开始和结束标签,只需要写
use NamespacePluginModelsNewsModel; //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>