编写纯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>
但是我在哪里可以设法为我的新闻文章制作某种类型的库?我已经阅读了有关在新插件中创建新类的内容,但我找不到任何有关此问题的教程或文档,或者我在搜索时只是使用了错误的术语。有人可以做一个小例子(可能有新闻文章)或发布我可以找到教程/文档的链接吗?
答案 0 :(得分:1)
答案 1 :(得分:1)
文档: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
这应该让你顺利,之后文档将会有所帮助。
答案 2 :(得分:0)
使用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>