Laravel入门

发布时间 2023-09-17 17:30:37作者: 心若向阳,次第花开

安装composer: 一个PHP项目的第三方库的管理工具

wget https://getcomposer.org/download/2.6.2/composer.phar
chmod a+x composer.phar
sudo mv composer.phar /usr/local/bin/composer
vi ~/.bashrc
source ~/.bashrc
export PATH=$HOME/.config/composer/vendor/bin:$PATH

查看项目使用到的第三方库

composer show

查看远程可用的第三方库

composer show laravel/breeze --available
composer global show

查看PHP命令的路径

whereis php

安装NVM: 一个NodeJS的多版本管理工具

sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | sudo bash
nvm install --lts

配置Composer的软件库地址,也有阿里云、华中等源,但是个人实践华为的好用,可能和我本地的网络有关系

composer config -g repos.packagist composer https://repo.huaweicloud.com/repository/php/

创建Laravel10的项目
composer create-project laravel/laravel laravel-tutorial-app
cd laravel-tutorial-app

安装最简单的身份认证库,会自动在控制器、实体类、前台视图、路由等地创建代码。

composer require laravel/breeze --dev
php artisan breeze:install

注册

http://localhost:8000/register

登录

http://localhost:8000/login

安装国际化库,支持中文
composer require laravel-lang/common
php artisan lang:add zh_CN


vi config/app.php
<?php

use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider;

return [

/*
|--------------------------------------------------------------------------
| Application Name
|--------------------------------------------------------------------------
|
| This value is the name of your application. This value is used when the
| framework needs to place the application's name in a notification or
| any other location as required by the application or its packages.
|
*/

'name' => env('APP_NAME', 'Laravel'),

/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services the application utilizes. Set this in your ".env" file.
|
*/

'env' => env('APP_ENV', 'production'),

/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/

'debug' => (bool) env('APP_DEBUG', false),

/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/

'url' => env('APP_URL', 'http://localhost'),

'asset_url' => env('ASSET_URL'),

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'Asia/Shanghai',

/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/

'locale' => 'zh_CN',

/*
|--------------------------------------------------------------------------
| Application Fallback Locale
|--------------------------------------------------------------------------
|
| The fallback locale determines the locale to use when the current one
| is not available. You may change the value to correspond to any of
| the language folders that are provided through your application.
|
*/

'fallback_locale' => 'en',

/*
|--------------------------------------------------------------------------
| Faker Locale
|--------------------------------------------------------------------------
|
| This locale will be used by the Faker PHP library when generating fake
| data for your database seeds. For example, this will be used to get
| localized telephone numbers, street address information and more.
|
*/

'faker_locale' => 'en_US',

/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/

'key' => env('APP_KEY'),

'cipher' => 'AES-256-CBC',

/*
|--------------------------------------------------------------------------
| Maintenance Mode Driver
|--------------------------------------------------------------------------
|
| These configuration options determine the driver used to determine and
| manage Laravel's "maintenance mode" status. The "cache" driver will
| allow maintenance mode to be controlled across multiple machines.
|
| Supported drivers: "file", "cache"
|
*/

'maintenance' => [
'driver' => 'file',
// 'store' => 'redis',
],

/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/

'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/

/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
])->toArray(),

/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/

'aliases' => Facade::defaultAliases()->merge([
// 'Example' => App\Facades\Example::class,
])->toArray(),

];

 

本人将MySQL Community Server安装在宿主机,没有安装在WSL2 Ubuntu里,后面配置数据库要用到。

查看宿主机ip,2个命令都可以: 172.29.240.1

ip route | grep default | awk '{print $3}'
cat /etc/resolv.conf | grep nameserver | awk '{print $2}'

创建数据库,配置账号及密码,还有权限

mysql -h http://172.29.240.1/ -u root -p

select @@version; -- 8.1.0 -- 查看数据库版本五年
create database if not exists laravel DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; -- 创建数据库

create user 'laravel'@'%' identified by 'qwas1209'; -- 创建账号和密码
grant all on laravel.* to 'laravel'@'%'; -- 授权
flush privileges; -- 刷新权限
show create database laravel; -- 查询创建数据库的sql

编辑Laravel的配置文件,设置数据库

vi .env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:G413lSChihWJ6TkSvepisUEMCL1I4gyPgZgY3rel3Y0=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=172.29.240.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=qwas1209

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUS

创建实体类:这个和Java区别挺大,不用定义属性

php artisan make:model Article --migration

 

INFO Model [app/Models/Article.php] created successfully.
INFO Migration [database/migrations/2023_09_16_133706_create_articles_table.php] created successfully.

 

vi 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;

// 和$request->post()结合简化绑定数据
protected $fillable = ['title', 'content'];
}

创建数据库移植代码
vi database/migrations/2023_09_15_013842_create_articles_table.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('articles', function (Blueprint $table) {
  if (!Schema::hasTable('articles')) {
    $table->id();
    $table->string('title');
    $table->tinytext('content');
    $table->tinyInteger('deleted')->default('0')->comment('删除标记:0:未删除, 1:删除');
    $table->timestamps();
  }
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('articles');
}
};


创建表
php artisan migrate

创建Controller
php artisan make:controller ArticleController --resource
INFO Controller [app/Http/Controllers/ArticleController.php] created successfully.

vi app/Http/Controllers/ArticleController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Article;
use Illuminate\Support\Facades\Log;

class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
  $articles = Article::orderBy('id', 'desc')->paginate(10);
  return view('articles.index', compact('articles'))->with('i', (request()->input('page', 1) - 1) * 10);
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('articles.create');
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
  $request->validate([
    'title' => 'required',
    'content' => 'required'
  ]);
  $article = new Article;
  /*
   * $article->title=$request->title;
   * $article->content=$request->content;
   * $article->save();
  */
  Article::create($request->post());
  return redirect()->route('articles.index')->with('operatedMessage', '成功新增文章.');
}

/**
* Display the specified resource.
*/
public function show(string $id)
{
  return view('articles.show', ['article' => Article::findOrFail($id)]);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
  return view('articles.edit', ['article' => Article::findOrFail($id)]);
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
  $request->validate([
    'title' => 'required',
    'content' => 'required'
  ]);
  $article = Article::find($id);
  /*
   * $article->title=$request->title;
   * $article->content=$request->content;
  */
  $article->fill($request->post());
  $article->save();
  return redirect()->route('articles.index')->with('operatedMessage', '成功修改文章.');
}

/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
  $article = Article::find($id);
  $article->delete();
  return redirect()->route('articles.index')->with('operatedMessage', '成功删除文章.');
}
}

创建前台视图页

mkdir resources/views/articles

vi resources/views/articles/create.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 10 CRUD入门</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>

<div class="container mt-2">

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left mb-2">
<h2>新增文章</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('articles.index') }}"> 返回</a>
</div>
</div>
</div>

@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
{{ session('status') }}
</div>
@endif

<form action="{{ route('articles.store') }}" method="POST" enctype="multipart/form-data">
@csrf

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>标题:</strong>
<input type="text" name="title" class="form-control" placeholder="标题">
@error('title')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>

<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>内容:</strong>
<textarea name="content" class="form-control" placeholder="内容"></textarea>
@error('content')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">新增</button>
</div>

</form>

</body>
</html>


vi resources/views/articles/edit.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 10 CRUD入门</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>

<div class="container mt-2">

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left mb-2">
<h2>编辑文章</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('articles.index') }}"> 返回</a>
</div>
</div>
</div>

@if(session('status'))
<div class="alert alert-success mb-1 mt-1">
{{ session('status') }}
</div>
@endif

<form action="{{ route('articles.update',$article->id) }}" method="POST">
@csrf
@method('PUT')

<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>标题:</strong>
<input type="text" name="title" class="form-control" placeholder="标题" value="{{$article->title}}">
@error('title')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>

<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>内容:</strong>
<textarea name="content" class="form-control" placeholder="内容">{{$article->content}}</textarea>
@error('content')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
@enderror
</div>
</div>
<button type="submit" class="btn btn-primary ml-3">保存</button>
</div>

</form>

</body>
</html>


vi resources/views/articles/index.blade.php
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Laravel 10 CRUD入门</title>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<style>
table {
border-spacing:0px;
border-collapse:collapse;
}

td,th {
boder:1px solid Black;
text-align: center;
}
tr:nth-child(odd) {
background-color:#bfa;
}
tr:hover{
background-color:yellow;
}
</style>
<body>

<div class="container mt-2">

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>文章管理</h2>
</div>
<div class="pull-right mb-2">
<a class="btn btn-success" href="{{ route('articles.create') }}">新增</a>
</div>
</div>
</div>

@if ($message = Session::get('operatedMessage'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif

<table class="table table-bordered">
<tr>
<th>序号</th>
<th>标题</th>
<th>内容</th>
<th>创建时间</th>
<th width="20px">操作</th>
</tr>
@foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->title }}</td>
<td>{{ $article->content }}</td>
<td>{{ $article->created_at }}</td>
<td>
<form action="{{ route('articles.destroy',$article->id) }}" method="Post">

<a class="btn btn-primary" href="{{ route('articles.edit',$article->id) }}">编辑</a>

@csrf
@method('DELETE')

<button type="submit" class="btn btn-danger">删除</button>
</form>
</td>
</tr>
@endforeach
</table>

{!! $articles->links() !!}

</body>
</html>

配置路由,设置访问地址
vi routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
return view('welcome');
});
Route::resource('articles', ArticleController::class);

Laravel对常用默认地址和方法的映射关系
GET/articles, mapped to the index() method,
GET /articles/create, mapped to the create() method,
POST /articles, mapped to the store() method,
GET /articles/{contact}, mapped to the show() method,
GET /articles/{contact}/edit, mapped to the edit() method,
PUT/PATCH /articles/{contact}, mapped to the update() method,
DELETE /articles/{contact}, mapped to the destroy() method.

解决分页布局乱的问题
php artisan vendor:publish --tag=laravel-pagination

vi app/Providers/AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
Paginator::useBootstrap();
}
}

运行项目

php artisan serve

访问项目

http://127.0.0.1:8000/articles