参考链接:https://laravel-news.com/your-first-laravel-application

简介

按照以下的步骤,你会创建一个简易的链接分享网站

安装 Laravel 安装器

composer global require "laravel/installer"

创建项目[1]

laravel new links

检查是否安装成功

访问地址:http://localhost/links/public/ 。看到欢迎页面,表示安装成功。

构建认证系统

执行命令:php artisan make:authphp artisan migrate[2]

> 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

现在系统里就有注册、登录功能了。

创建 Model & 插入伪数据

创建 Model

创建迁移文件

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();
});

执行迁移

php artisan migrate

创建 Link 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 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↩︎

一步一步带你构建第一个 Laravel 项目的更多相关文章

  1. 【简书】在阿里云自带的CentOS + LAMP环境下部署一个Laravel项目

    在阿里云自带的CentOS + LAMP环境下部署一个Laravel项目 作者 DonnieZero 关注 2017.07.29 22:02* 字数 2218 阅读 5556评论 3喜欢 1赞赏 1 ...

  2. 创建一个 Laravel 项目

    创建一个 Laravel 项目,首先需要安装 Composer ,如果没有安装的参考 https://docs.phpcomposer.com/00-intro.html 一.安装 Laravel 安 ...

  3. 构建你人生里的第一个 Laravel 项目

    安装 Laravel 安装器 composer global require "laravel/installer" 创建项目 laravel new links 检查是否安装成功 ...

  4. Gradle入门(3):构建第一个Java项目

    Gradle插件通过引入特定领域的约定和任务来构建你的项目.Java插件是Gradle自身装载的一个插件.Java插件提供的基本功能远比源代码编译和打包多.它为你的项目建立了一个标准的项目布局,并确保 ...

  5. AgileBoot - 手把手一步一步带你Run起全栈项目(SpringBoot+Vue3)

    AgileBoot是笔者在业余时间基于ruoyi改造优化的前后端全栈项目. 关于AgileBoot的详细介绍:https://www.cnblogs.com/valarchie/p/16777336. ...

  6. 使用maven构建第一个web项目

    在eclipse中,正常创建maven项目后,发现在index.jsp中会报错,此时在pom.xml中加入如下依赖关系即可 <dependency> <groupId>java ...

  7. 完成一个Laravel项目的过程

    1.分析项目,找出项目的元素并进行建模(navicat 该工具还可以到处sql语句) 建立关系 2.安装Laravel(使用composer来安装,如果没有的话先安装composer) 3.配置虚拟主 ...

  8. Eclipse的maven构建一个web项目,以构建SpringMVC项目为例

    http://www.cnblogs.com/javaTest/archive/2012/04/28/2589574.html springmvc demo实例教程源代码下载:http://zuida ...

  9. 推荐一个 Laravel admin 后台管理插件

    如何优雅的写代码,我想是每位程序员的心声.自从15年初第一次接触 Laravel 4.2 开始,我就迷上使用 Laravel 框架了.我一直都想找个时间好好写写有关 Laravel 的使用文章,由浅入 ...

随机推荐

  1. 关于ant及svnant的一点随记

    在使用svnant的时候: 注意一下: 1.JDK版本,svnant目前更新到1.3.1,其中svnkit.jar是不支持1.7/1.8JDK的,容易出现各种错误 Ps:下载http://www.sv ...

  2. Java之父职场路

    Java之父——詹姆斯·高斯林出生于加拿大,是一位计算机编程天才.在卡内基·梅隆大学攻读计算机博士学位时,他编写了多处理器版本的Unix操作系统,是JAVA编程语言的创始人.1991年,在Sun公司工 ...

  3. Tomcat设置欢迎页问题

    今天下载了tomat9,配置到eclipse后拉起来,想跑个欢迎页看看是否起好了,随手写了个index.jsp放到项目Struts2的WebContent根目录下,直接打开网页输入http://loc ...

  4. 关于在windows server 2008 上部署wampserver2.5部署的问题

    1.关闭windows自带防火墙 2.httpd.conf文件权限 apache 2.4.9 外网访问的问题参考此文: http://blog.csdn.net/lysc_forever/articl ...

  5. zabbix监控的配置

    ZABBIX监控的操作步骤有两个! 首先登录到zabbix 的主界面在configuration---host---create host在如上的host 创建界面中 主要是输入被监测的server的 ...

  6. 使用Collections类对 集合排序

    对Set<Object>集合进行排序 根据类型的某一个属性去排序 public Set<School> sortByValue(Set<School> set){ ...

  7. Manager Test and DAO

    1. 阅读ManagerTest代码 (1)代码 import java.util.* package test; /** * This program demonstrates inheritanc ...

  8. 借助curl理解$GLOBALS['HTTP_RAW_POST_DATA'] ,$_POST, php://input

    发送请求代码 post.php <?php $url='http://localhost/web/curl/url.php'; $data='a=123|b=2&c=3'; $heade ...

  9. git 批量删除本地分支

    git branch | grep 'bug' |xargs git branch -D

  10. JavaScript语言基础-作用域