HackMD
    • Sharing Link copied
    • /edit
    • View mode
      • Edit mode
      • View mode
      • Book mode
      • Slide mode
      Edit mode View mode Book mode Slide mode
    • Note Permission
    • Read
      • Only me
      • Signed-in users
      • Everyone
      Only me Signed-in users Everyone
    • Write
      • Only me
      • Signed-in users
      • Everyone
      Only me Signed-in users Everyone
    • More (Comment, Invitee)
    • Publishing
    • Commenting Enable
      Disabled Forbidden Owners Signed-in users Everyone
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Invitee
    • No invitee
    • Options
    • Versions
    • Transfer ownership
    • Delete this note
    • Template
    • Save as template
    • Insert from template
    • Export
    • Google Drive Export to Google Drive
    • Gist
    • Import
    • Google Drive Import from Google Drive
    • Gist
    • Clipboard
    • Download
    • Markdown
    • HTML
    • Raw HTML
Menu Sharing Help
Menu
Options
Versions Transfer ownership Delete this note
Export
Google Drive Export to Google Drive Gist
Import
Google Drive Import from Google Drive Gist Clipboard
Download
Markdown HTML Raw HTML
Back
Sharing
Sharing Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
More (Comment, Invitee)
Publishing
More (Comment, Invitee)
Commenting Enable
Disabled Forbidden Owners Signed-in users Everyone
Permission
Owners
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Invitee
No invitee
   owned this note    owned this note      
Published Linked with
Like BookmarkBookmarked
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
以下使用 Laravel 10 框架實作網路討論區的主要程式碼與說明: 🛠️ 一、資料庫結構(使用 migrations) 1. users 資料表遷移文件: ```php // database/migrations/xxxx_create_users_table.php Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->string('avatar')->nullable(); $table->enum('role', ['admin', 'moderator', 'user'])->default('user'); $table->text('bio')->nullable(); $table->rememberToken(); $table->timestamps(); }); // boards 資料表 Schema::create('boards', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('description'); $table->timestamps(); }); // posts 資料表 Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->longText('content'); $table->foreignId('user_id')->constrained(); $table->foreignId('board_id')->constrained(); $table->timestamps(); }); ``` 🔐 二、後端邏輯(Controllers) 1. 會員控制器: ```php // app/Http/Controllers/UserController.php public function updateProfile(Request $request) { $user = auth()->user(); $validated = $request->validate([ 'name' => 'required|string|max:255', 'avatar' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 'bio' => 'nullable|string|max:500' ]); if ($request->hasFile('avatar')) { $path = $request->file('avatar')->store('avatars', 'public'); $validated['avatar'] = $path; } $user->update($validated); return redirect()->route('profile')->with('success', '資料已更新'); } ``` 2. 文章控制器: ```php // app/Http/Controllers/PostController.php public function store(Request $request, Board $board) { $request->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string' ]); $post = new Post($request->only('title', 'content')); $post->user_id = auth()->id(); $board->posts()->save($post); return redirect()->route('posts.show', $post); } ``` 🌐 三、前端頁面(使用 Blade 模板) 1. 註冊頁面片段: ```php // resources/views/auth/register.blade.php <form method="POST" action="{{ route('register') }}"> @csrf <input id="name" type="text" name="name" required autofocus> <input id="email" type="email" name="email" required> <input id="password" type="password" name="password" required> <input id="password-confirm" type="password" name="password_confirmation" required> <button type="submit">註冊</button> </form> ``` 2. 文章編輯器(含圖片上傳): ```php // resources/views/posts/create.blade.php <form method="POST" action="{{ route('posts.store', $board) }}"> @csrf <input type="text" name="title" required> <textarea id="editor" name="content"></textarea> <button type="submit">發表文章</button> </form> @push('scripts') <script src="https://cdn.tiny.cloud/1/your-api-key/tinymce/6/tinymce.min.js"></script> <script> tinymce.init({ selector: '#editor', plugins: 'image code', toolbar: 'undo redo | image code', images_upload_url: '{{ route('upload.image') }}', images_upload_credentials: true }); </script> @endpush ``` 🔗 四、路由設定 ```php // routes/web.php Auth::routes(); Route::middleware(['auth'])->group(function () { // 會員相關 Route::get('/profile', [UserController::class, 'show'])->name('profile'); Route::put('/profile', [UserController::class, 'update']); // 討論區 Route::resource('boards', BoardController::class)->only(['index', 'show']); // 文章 Route::resource('posts', PostController::class)->except(['index']); Route::post('/upload-image', [PostController::class, 'uploadImage']); }); // 管理員路由 Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () { Route::resource('users', Admin\UserController::class); }); ``` 🛡️ 五、權限控制(Middleware) ```php // app/Http/Middleware/AdminMiddleware.php public function handle(Request $request, Closure $next) { if (auth()->check() && auth()->user()->role === 'admin') { return $next($request); } abort(403); } ``` 🔧 六、圖片上傳處理 ```php // app/Http/Controllers/PostController.php public function uploadImage(Request $request) { $request->validate(['file' => 'required|image|max:2048']); $path = $request->file('file')->store('post-images', 'public'); return response()->json([ 'location' => asset("storage/$path") ]); } ``` 📦 七、輔助功能 1. 使用者模型關係: ```php // app/Models/User.php public function posts() { return $this->hasMany(Post::class); } ``` 2. 分頁處理: ```php // app/Http/Controllers/BoardController.php public function show(Board $board) { $posts = $board->posts()->with('user')->paginate(20); return view('boards.show', compact('board', 'posts')); } ``` 📝 系統說明: 1. 主要功能結構: - 會員系統:使用 Laravel Breeze 擴充套件基礎功能 - 權限系統:三層級角色控制(admin/moderator/user) - 討論區:多個獨立討論板塊 - 文章系統:富文本編輯器支援圖片上傳 2. 前端技術: - Blade 模板引擎 - Bootstrap 5 框架 - TinyMCE 富文本編輯器 - 響應式網頁設計 3. 安全機制: - CSRF 保護 - XSS 防護(使用 HTML Purifier) - 表單請求驗證 - 檔案上傳類型限制 - 密碼雜湊儲存 4. 資料庫關聯: - 使用者(1) → 文章(N) - 討論板(1) → 文章(N) - 文章(1) → 回覆(N)(可擴充) 5. 部署注意事項: - 需執行 `php artisan migrate` - 設定 storage 目錄權限 - 設定 .env 檔案中的 MAIL 與 APP_KEY - 建議使用隊列處理圖片上傳等耗時操作 6. 擴充建議: - 加入文章分類標籤功能 - 實作文章收藏系統 - 新增站內訊息通知 - 整合社群媒體登入功能 此架構遵循 Laravel 最佳實踐,採用 MVC 模式分離各層邏輯,適合中大型討論區開發需求,可根據實際需求進行模組擴充。

Import from clipboard

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lost their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.


Upgrade

All
  • All
  • Team
No template.

Create a template


Upgrade

Delete template

Do you really want to delete this template?

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

Sign in via GitHub

New to HackMD? Sign up

Help

  • English
  • 中文
  • 日本語

Documents

Tutorials

Book Mode Tutorial

Slide Example

YAML Metadata

Resources

Releases

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions

Versions

Upgrade now

Version named by    

More Less
  • Edit
  • Delete

Note content is identical to the latest version.
Compare with
    Choose a version
    No search result
    Version not found

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.