Of course. Let's break down view.php in the context of the DedeCMS (织梦内容管理系统) framework.

view.php is one of the most critical and frequently accessed files in a DedeCMS website. Its primary and sole purpose is to display the full content of a single article.
Think of it as the "Article Detail Page" or "Post Page" generator. When a user clicks on the title of an article in a list (like on the homepage, a category page, or a search results page), the link they click usually points to view.php.
Core Functionality
At its heart, view.php does the following:
-
Receives an ID: It gets the unique ID of the article to be displayed from the URL. This is typically passed as a query string parameter, most commonly
aid(Article ID).
(图片来源网络,侵删)- Example URL:
http://your-site.com/view.php?aid=123
- Example URL:
-
Queries the Database: It connects to the DedeCMS database and runs a query to fetch all the necessary information for that specific article from the
dede_archivestable (and potentially related tables likedede_arctypefor category info). -
Increments View Count: A crucial step is to increment the view count (
clickfield) for that article in the database. This is how DedeCMS tracks the popularity of each piece of content. -
Loads a Template: It identifies which template file to use for displaying the article. This is determined by the article's
typeid(category ID) and the channel settings. It loads the corresponding HTML/PHP template file (e.g.,article_article.htm). -
Processes and Displays Content: It takes the data from the database (title, body, author, publish date, etc.) and "parses" it into the template. This involves replacing template variables like
{dede:field.title/}with the actual data. It also processes any special DedeCMS tags (like{dede:field.body/}which handles the article content, including images and other media). -
Outputs the Final HTML: The final, fully rendered HTML page is sent to the user's browser.
The Code Breakdown (A Simplified View)
While the actual view.php file can be complex due to security checks, SEO functions, and plugin support, here is a simplified, conceptual version of what it does internally.
<?php
// 1. 引入核心文件 (Include core files)
// This loads all necessary DedeCMS functions and configurations.
require_once(dirname(__FILE__)."/include/common.inc.php");
// 2. 获取文章ID (Get the Article ID from the URL)
// It safely retrieves the 'aid' parameter. If not set, it shows an error.
$aid = isset($aid) && is_numeric($aid) ? $aid : 0;
if($aid == 0) {
ShowMsg('您指定的文档不存在!', '-1');
exit();
}
// 3. 查询数据库 (Query the database for the article)
// This SQL query selects all fields for the given article ID.
$row = $dsql->GetOne("SELECT * FROM `#@__archives` WHERE id='$aid'");
// 4. 检查文章是否存在 (Check if the article exists)
if(!is_array($row)) {
ShowMsg('您指定的文档不存在或已删除!', '-1');
exit();
}
// 5. 增加浏览次数 (Increment the view count)
// This is a key feature for tracking popularity.
$dsql->ExecuteNoneQuery("UPDATE `#@__archives` SET click=click+1 WHERE id='$aid'");
// 6. 获取模板并处理内容 (Get the template and process content)
// It gets the template associated with the article's channel and type.
$artUrl = GetFileUrl($aid,$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],$row['arcrank'],$row['channel'],$row['money']);
$templet = GetTemplet($row['typeid']);
// 7. 解析模板并输出 (Parse the template and output)
// This loads the template file and replaces all {dede:...} tags with data.
$p = new PartView();
$p->SetTemplet($templet);
$p->Display();
?>
Key Variables and Tags Used in the Template
The view.php script works in tandem with its template file (e.g., article_article.htm). Here are the most common DedeCMS tags you'll find in that template, which are populated by view.php:
| Template Tag | Description |
|---|---|
{dede:field.title/} |
Displays the full title of the article. |
{dede:field.pubdate function='strftime("%Y-%m-%d %H:%M:%S",@me)'/} |
Displays the publication date, formatted using PHP's strftime function. |
{dede:field.source/} |
Displays the source of the article. |
{dede:field.writer/} |
Displays the author's name. |
{dede:field.body/} |
This is the most important tag. It displays the full main content of the article, including all text, images, and embedded media. |
{dede:field.description/} |
Displays the article's summary or description. |
{dede:pagelist listsize='4' listitem='index pre pageno next end '/} |
Generates a page list for articles that are split into multiple pages. |
{dede:field.id/} |
Displays the unique ID of the article. |
{dede:field.typeid/} |
Displays the ID of the category the article belongs to. |
{dede:php}...{/dede:php} |
Allows you to execute custom PHP code within the template. |
SEO and URL Rewriting
By default, DedeCMS uses view.php?aid=123, which is not SEO-friendly. To improve this, DedeCMS uses URL rewriting.
- How it works: You enable URL rewriting in the DedeCMS backend (System -> System Basic Settings -> Core Option). This usually involves creating or modifying a
.htaccessfile on your Apache server orweb.configon IIS. - The Result: The ugly
view.php?aid=123URL is transformed into a clean, readable one like:http://your-site.com/a/123/abc.html(whereabcis a pinyin or English version of the title)http://your-site.com/html/2025/10/123.html
Even when you use a pretty URL, the request is still internally handled by view.php. The rewriting rule just hides the view.php part from the user and search engines.
How to Customize view.php
While you can directly edit view.php, it's often better to use the template system for most changes.
- Changing the Layout: Edit the corresponding template file (e.g.,
article_article.htm). This is the recommended method for changing the look and feel of the article page. - Adding New Fields: If you've added custom fields to your articles (e.g., "reading_time", "author_bio"), you can display them in the template using
{dede:field.your_custom_field_name/}. - Modifying Core Logic: If you need to fundamentally change how the page works (e.g., change how the view count is incremented, add a custom query), you would need to edit
view.phpdirectly. Always back up the file before making changes.
Summary
| Aspect | Description |
|---|---|
| Purpose | To display the full content of a single article. |
| Key Input | Article ID (aid) from the URL. |
| Key Action | Fetches article data from the database and increments its view count. |
| Partner | A template file (e.g., article_article.htm) that defines the page's layout. |
| URL | Typically view.php?aid=XXX, but often rewritten for SEO (e.g., /a/XXX/xxx.html). |
| Customization | Primarily done by editing the template file, not view.php itself. |
