安裝 Laravel 安裝器
composer global require "laravel/installer"
創建項目
laravel new links
檢查是否安裝成功
訪問地址:http://localhost/links/public/。看到歡迎頁面,表示安裝成功。
構建認證系統
執行命令:php artisan make:auth
和php artisan migrate
。
> 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
然後,修改文件 resource/views/layouts/app.blade.php
。
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<script src="{{ asset('js/app.js') }}"></script>
現在系統裡就有註冊、登錄功能了。
創建 Model & 插入數據
創建 Model
創建遷移文件
php artisan make:migration create_links_table --create=links
遷移文件內容
Schema::create('links', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('url')->unique;
$table->text('description');
$table->timestamps;
});
執行遷移
php artisan migrate
創建 Model
php artisan make:model Link
在 ModelFactory.php
中為 Link Model 定義工廠方法
$factory->define(App\Link::class, function (Faker\Generator $faker) {
return [
'title' => $faker->name,
'url' => $faker->url,
'description' => $faker->paragraph,
];
});
創建種子文件 LinksTableSeeder.php
php artisan make:seeder LinksTableSeeder
在種子文件中使用工廠方法
public function run
{
factory(App\Link::class, 10)->create;
}
在 DatabaseSeeder.php
中註冊種子文件
$this->call(LinksTableSeeder::class);
執行種子文件[^注一]
php artisan db:seed
路由和視圖
// routes/web.php
Route::get('/', function {
$links = \App\Link::all;
return view('welcome', ['links' => $links]);
});
// resources/views/welcome.blade.php
<div class="links">
@foreach ($links as $link)
<a href="{{ $link->url }}">{{ $link->title }}</a>
@endforeach
</div>
數據提交
路由 link/submit
use Illuminate\Http\Request;
Route::get('link/submit', function {
return view('link.submit');
});
Route::post('link/submit', function(Request $request) {
$validator = Validator::make($request->all, [
'title' => 'required|max:255',
'url' => 'required|max:255',
'description' => 'required|max:255',
]);
if ($validator->fails) {
return back
->withInput
->withErrors($validator);
}
$link = new \App\Link;
$link->title = $request->title;
$link->url = $request->url;
$link->description = $request->description;
$link->save;
return redirect('/');
});
視圖 resources/views/link/submit.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('link/submit') }}" method="post">
{!! csrf_field !!}
<div class="form-group">
<label for="title">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">Description</label>
<textarea class="form-control" id="description" name="description" placeholder="description">{{ old('description') }}</textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
@endsection
tags: Laravel
項目
相關推薦
推薦中...