当前位置:实例文章 » JAVA Web实例» [文章]用thinkphp6写一个博客系统,输出完整项目代码

用thinkphp6写一个博客系统,输出完整项目代码

发布人:shili8 发布时间:2025-02-08 07:03 阅读次数:0

**ThinkPHP6 博客系统**

###项目结构

bashblog-system/
app/
BaseController.phpBlogController.phpCategoryController.phpTagController.phpUserController.phpconfig/
database.phproute.phpmodel/
Blog.phpCategory.phpTag.phpUser.phppublic/
index.phpvendor/
...


### **数据库配置**

在 `config/database.php` 中配置数据库连接信息:

phpreturn [
 'type' => 'mysql',
 'hostname' => 'localhost',
 'database' => 'blog_system',
 'username' => 'root',
 'password' => '',
 'charset' => 'utf8',
 'prefix' => '',
];


### **路由配置**

在 `config/route.php` 中定义路由规则:

phpreturn [
 // ...
 'GET /' => ['BlogController', 'index'],
 'GET /blog/:id' => ['BlogController', 'show'],
 'GET /category/:id' => ['CategoryController', 'show'],
 'GET /tag/:id' => ['TagController', 'show'],
 'POST /login' => ['UserController', 'login'],
 // ...
];


### **模型定义**

在 `model/Blog.php` 中定义博客模型:

phpnamespace appmodel;

use thinkModel;

class Blog extends Model{
 protected $table = 'blog';
 protected $autoWriteTimestamp = true;
}


同样地,定义 `Category`, `Tag`, 和 `User` 模型。

### **控制器**

#### **BlogController**

phpnamespace appcontroller;

use thinkController;
use appmodelBlog;

class BlogController extends Controller{
 public function index()
 {
 $blogs = Blog::all();
 return view('blog/index', ['blogs' => $blogs]);
 }

 public function show($id)
 {
 $blog = Blog::find($id);
 if (!$blog) {
 return 'Blog not found';
 }
 return view('blog/show', ['blog' => $blog]);
 }
}


#### **CategoryController**

phpnamespace appcontroller;

use thinkController;
use appmodelCategory;

class CategoryController extends Controller{
 public function show($id)
 {
 $category = Category::find($id);
 if (!$category) {
 return 'Category not found';
 }
 return view('category/show', ['category' => $category]);
 }
}


#### **TagController**

phpnamespace appcontroller;

use thinkController;
use appmodelTag;

class TagController extends Controller{
 public function show($id)
 {
 $tag = Tag::find($id);
 if (!$tag) {
 return 'Tag not found';
 }
 return view('tag/show', ['tag' => $tag]);
 }
}


#### **UserController**

phpnamespace appcontroller;

use thinkController;
use appmodelUser;

class UserController extends Controller{
 public function login()
 {
 if (request()->isPost()) {
 // Login logic here }
 return view('user/login');
 }
}


### **视图**

在 `public/index.php` 中定义视图:

phpreturn [
 'blog/index' => 'blog/index.html',
 'blog/show' => 'blog/show.html',
 'category/show' => 'category/show.html',
 'tag/show' => 'tag/show.html',
 'user/login' => 'user/login.html',
];


### **完整代码**

php// app/BaseController.phpnamespace appBase;

use thinkController;
use thinkRequest;

class BaseController extends Controller{
 protected $request;

 public function __construct(Request $request)
 {
 $this->request = $request;
 }
}

// app/BlogController.phpnamespace appcontroller;

use thinkController;
use appmodelBlog;

class BlogController extends Controller{
 public function index()
 {
 $blogs = Blog::all();
 return view('blog/index', ['blogs' => $blogs]);
 }

 public function show($id)
 {
 $blog = Blog::find($id);
 if (!$blog) {
 return 'Blog not found';
 }
 return view('blog/show', ['blog' => $blog]);
 }
}

// app/CategoryController.phpnamespace appcontroller;

use thinkController;
use appmodelCategory;

class CategoryController extends Controller{
 public function show($id)
 {
 $category = Category::find($id);
 if (!$category) {
 return 'Category not found';
 }
 return view('category/show', ['category' => $category]);
 }
}

// app/TagController.phpnamespace appcontroller;

use thinkController;
use appmodelTag;

class TagController extends Controller{
 public function show($id)
 {
 $tag = Tag::find($id);
 if (!$tag) {
 return 'Tag not found';
 }
 return view('tag/show', ['tag' => $tag]);
 }
}

// app/UserController.phpnamespace appcontroller;

use thinkController;
use appmodelUser;

class UserController extends Controller{
 public function login()
 {
 if (request()->isPost()) {
 // Login logic here }
 return view('user/login');
 }
}


以上是完整的ThinkPHP6博客系统代码示例。

相关标签:android
其他信息

其他资源

Top