laravel开发新模块案例ChatGPT

  • laravel开发新模块案例
  • ChatGPT

    2024年06月03日 10时13分Token: 1528

    好的,以下是一个关于如何在Laravel应用中开发一个新模块的案例。这个示例将演示如何创建一个简单的博客模块,包括创建与管理文章的基本功能。

    首先,确保你已经安装Laravel

    复制代码
    composer create-project --prefer-dist laravel/laravel blogApp
    cd blogApp

    1. 创建迁移文件和模型

    我们需要一个数据库表来存储文章。使用Laravel的artisan命令来生成迁移和模型。

    复制代码
    php artisan make:model Article -m

    这个命令会创建app/Models/Article.php模型和database/migrations/xxxx_xx_xx_create_articles_table.php迁移文件。

    2. 定义数据库迁移

    打开database/migrations/xxxx_xx_xx_create_articles_table.php文件,并定义表结构:

    php 复制代码
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

    然后运行迁移:

    复制代码
    php artisan migrate

    3. 设置模型

    编辑app/Models/Article.php文件,为文章模型添加一些必要配置:

    php 复制代码
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    class Article extends Model
    {
        use HasFactory;
    
        protected $fillable = ['title', 'content'];
    }

    4. 创建控制器

    创建一个控制器来处理文章的基本操作:

    复制代码
    php artisan make:controller ArticleController

    app/Http/Controllers/ArticleController.php中添加以下内容:

    php 复制代码
    namespace App\Http\Controllers;
    
    use App\Models\Article;
    use Illuminate\Http\Request;
    
    class ArticleController extends Controller
    {
        public function index()
        {
            $articles = Article::all();
            return view('articles.index', compact('articles'));
        }
    
        public function create()
        {
            return view('articles.create');
        }
    
        public function store(Request $request)
        {
            $validated = $request->validate([
                'title' => 'required|string|max:255',
                'content' => 'required|string',
            ]);
    
            Article::create($validated);
    
            return redirect()->route('articles.index');
        }
    
        public function show(Article $article)
        {
            return view('articles.show', compact('article'));
        }
    
        public function edit(Article $article)
        {
            return view('articles.edit', compact('article'));
        }
    
        public function update(Request $request, Article $article)
        {
            $validated = $request->validate([
                'title' => 'required|string|max:255',
                'content' => 'required|string',
            ]);
    
            $article->update($validated);
    
            return redirect()->route('articles.index');
        }
    
        public function destroy(Article $article)
        {
            $article->delete();
    
            return redirect()->route('articles.index');
        }
    }

    5. 定义路由

    routes/web.php中定义路由:

    php 复制代码
    use App\Http\Controllers\ArticleController;
    
    Route::resource('articles', ArticleController::class);

    6. 创建视图

    resources/views目录下,创建一个articles文件夹,并添加以下视图文件。

    index.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>Articles</h1>
        <a href="{{ route('articles.create') }}">Create New Article</a>
        @foreach ($articles as $article)
            <div>
                <h2><a href="{{ route('articles.show', $article) }}">{{ $article->title }}</a></h2>
                <p>{{ Illuminate\Support\Str::limit($article->content, 100) }}</p>
                <a href="{{ route('articles.edit', $article) }}">Edit</a>
                <form action="{{ route('articles.destroy', $article) }}" method="POST" style="display: inline;">
                    @csrf
                    @method('DELETE')
                    <button type="submit">Delete</button>
                </form>
            </div>
        @endforeach
    @endsection
    create.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>Create New Article</h1>
        <form action="{{ route('articles.store') }}" method="POST">
            @csrf
            <div>
                <label for="title">Title:</label>
                <input type="text" name="title" id="title" value="{{ old('title') }}">
                @error('title')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <div>
                <label for="content">Content:</label>
                <textarea name="content" id="content">{{ old('content') }}</textarea>
                @error('content')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <button type="submit">Create</button>
        </form>
    @endsection
    show.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>{{ $article->title }}</h1>
        <p>{{ $article->content }}</p>
        <a href="{{ route('articles.index') }}">Back to Articles</a>
    @endsection
    edit.blade.php
    php 复制代码
    @extends('layouts.app')
    
    @section('content')
        <h1>Edit Article</h1>
        <form action="{{ route('articles.update', $article) }}" method="POST">
            @csrf
            @method('PUT')
            <div>
                <label for="title">Title:</label>
                <input type="text" name="title" id="title" value="{{ old('title', $article->title) }}">
                @error('title')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <div>
                <label for="content">Content:</label>
                <textarea name="content" id="content">{{ old('content', $article->content) }}</textarea>
                @error('content')
                    <div>{{ $message }}</div>
                @enderror
            </div>
            <button type="submit">Update</button>
        </form>
    @endsection

    7. 运行应用

    使用以下命令启动开发服务器:

    复制代码
    php artisan serve

    访问http://localhost:8000/articles,你应该能够看到文章的列表页面并可以进行创建、查看、编辑和删除操作。

    以上就是一个简单的Laravel博客模块开发案例。希望这能帮助你理解如何在Laravel中创建新的模块。

相关推荐
stand_forever2 天前
laravel框架优化
php·laravel
Python涛哥2 天前
PHP框架之Laravel框架教程:1. laravel搭建
开发语言·php·laravel
~央千澈~7 天前
Laravel 系统版本查看及artisan管理员密码找回方法针对各个版本通用方法及原理-优雅草卓伊凡
php·laravel
小小怪下士yeah7 天前
Laravel 静态方法的合理使用考量【超详细】
android·java·laravel
Rocket MAN8 天前
Laravel 原子锁概念讲解
php·wpf·laravel
ONLYOFFICE11 天前
如何将 ONLYOFFICE 文档集成到使用 Laravel 框架编写的 PHP 网络应用程序中
php·laravel
四季豆豆豆16 天前
博客项目 laravel vue mysql 第四章 分类功能
vue.js·mysql·laravel
yanwushu24 天前
10分钟搭建 PHP 开发环境教程
php·laravel
RainSerein25 天前
Laravel8中使用phpword生成word文档
word·php·laravel
RainSerein25 天前
Laravel8中调取腾讯云文字识别OCR
ocr·php·腾讯云·laravel