一步一步帶你構建第一個 Laravel 項目

參考連接:https://laravel-news.com/your-first-laravel-applicationphp

簡介

按照如下的步驟,你會建立一個簡易的連接分享網站css

安裝 Laravel 安裝器

composer global require "laravel/installer"

建立項目1

laravel new links

檢查是否安裝成功

訪問地址:http://localhost/links/public/ 。看到歡迎頁面,表示安裝成功。mysql

構建認證系統

執行命令:php artisan make:authphp artisan migrate2laravel

> php artisan make:auth
Authentication scaffolding generated successfully.
> php artisan migrate
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

如今系統裏就有註冊、登陸功能了。web

建立 Model & 插入僞數據

建立 Model

建立遷移文件sql

php artisan make:migration create_links_table --create=links

寫遷移文件的 up 方法數據庫

Schema::create('links', function (Blueprint $table) {
      $table->increments('id');
      $table->string('title');
      $table->string('url')->unique();
      $table->text('description');
      $table->timestamps();
});

執行遷移app

php artisan migrate

建立 Link Modelcomposer

php artisan make:model Link

ModelFactory.php 中爲 Link Model 定義工廠方法post

$factory->define(App\Link::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->name,
        'url' => $faker->url,
        'description' => $faker->paragraph,
    ];
});

建立種子文件 LinksTableSeeder

php artisan make:seeder LinksTableSeeder

在種子文件中使用工廠方法

public function run()
{
    factory(App\Link::class, 10)->create();
}

DatabaseSeeder 中註冊種子文件

$this->call(LinksTableSeeder::class);

執行種子文件3

php artisan db:seed

路由和視圖

routes/web.php 中添加路由——首頁、建立頁和保存連接。

use App\Link;
use Illuminate\Http\Request;

Route::group(['prefix' => 'links'], function () {
    Route::get('', function () {
        $links = Link::all();
        return view('links.index', compact('links'));
    });
    Route::get('create', function () {
        return view('links.create');
    });
    Route::post('store', function(Request $request) {
        $validator = Validator::make($request->all(), [
            'title' => 'required|max:255',
            'url' => 'required|max:255',
            'description' => 'nullable|max:255',
        ]);
        if ($validator->fails()) {
            return back()
                ->withInput()
                ->withErrors($validator);
        }
        $link = new Link();
        $link->title = $request->title;
        $link->url = $request->url;
        $link->description = $request->description;
        $link->save();
        return redirect('/links');
    });
});

resources/views/links 添加兩個視圖文件。

  1. 連接分享的首頁index.blade.php
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading"><h1>Links Sharing</h1></div>

                <div class="panel-body">
                    <div class="row">
                        @foreach ($links as $link)
                            <div class="col-sm-6 col-md-4">
                                <div class="thumbnail">
                                    <div class="caption">
                                        <h2><a href="{{ $link->url }}" target="_blank">{{ $link->title }}</a></h3>
                                        <p>{{ $link->description  }}</p>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
  1. 建立連接頁create.blade.php
@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <h1>Submit a link</h1>

            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form action="{{ url('links/store') }}" method="post">
                {{ csrf_field() }}
                <div class="form-group">
                    <label for="title">標題</label>
                    <input type="text" class="form-control" id="title" name="title" placeholder="Title" value="{{ old('title') }}">
                </div>
                <div class="form-group">
                    <label for="url">URL</label>
                    <input type="text" class="form-control" id="url" name="url" placeholder="URL" value="{{ old('url') }}">
                </div>
                <div class="form-group">
                    <label for="description">介紹</label>
                    <textarea class="form-control" id="description" name="description" placeholder="description">{{ old('description') }}</textarea>
                </div>
                <button type="submit" class="btn btn-default">建立</button>
            </form>
        </div>
    </div>
@endsection
tags: Laravel 項目

  1. 項目數據庫名使用 laravel-links,採用 utf8mb4_unicode_ci 校對。修改 MySQL 配置文件 mysql.ini(Windows 環境下) 將 default-storage-engine 項設置爲 InnoDB——表示新建數據庫表的默認存儲引擎使用 InnoDB。

  2. 對於 Laravel 5.3- 版本,須要修改文件 resource/views/layouts/app.blade.php。將引入的 JavaScript 和 CSS 文件的地址改成 <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <script src="{{ asset('js/app.js') }}"></script>

  3. 也能夠在遷移時執行種子文件,命令是 php artisan migrate --seed

相關文章
相關標籤/搜索