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中创建新的模块。

相关推荐
bubiyoushang8881 天前
深入探索Laravel框架中的Blade模板引擎
android·android studio·laravel
tianlebest3 天前
Laravel 安全:批量赋值 fillable 与 guarded
数据库·安全·laravel
深山技术宅4 天前
在Laravel 12中实现基于parent_id的树状数组
php·laravel
深山技术宅7 天前
在 Laravel 12 中实现 WebSocket 通信
websocket·php·laravel
深山技术宅7 天前
laravel 12 监听syslog消息,并将消息格式化后存入mongodb
mongodb·php·laravel
深山技术宅10 天前
Laravel 12 实现 API 登录令牌认证
php·laravel
深山技术宅11 天前
Laravel 12 实现 OAuth2 登录
php·laravel
Taichi呀11 天前
Laravel+API 接口
php·laravel
Taichi呀13 天前
Laravel基础
php·laravel
foryoufeng17 天前
Laravel代码生成器,快速构建项目代码,统一团队编码风格
php·laravel