首页 > 其他分享 >Laravel入门

Laravel入门

时间:2023-09-17 17:33:06浏览次数:40  
标签:Laravel articles 入门 APP application --------------------------------------------

安装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="[email protected]"
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

标签:Laravel,articles,入门,APP,application,--------------------------------------------
From: https://www.cnblogs.com/terry-cc/p/17709272.html

相关文章

  • PHP入门-搭建运行环境
    在系统装好后,推荐将电脑的命名一个言简意赅的短小名称,这样在Linuxshell或Windows命令行下,看着更舒服,特别是输入的内容较长时,效果会更明显。最近被安排接手PHP项目,由于没接触过,所以做个记录整理,便于以后查看。不管做什么开发,搭建环境是第一步。1. XMAPP 是Apache+MySQL+PHP+PE......
  • Spring boot data mongodb入门
    在SpringBoot中使用Mongodb,首先导入依赖库:    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-data-mongodb</artifactId>    </dependency>连接配置## 阿里云副本实......
  • Linux 入门笔记
    一开始对linux总有些抵触,黑黑的命令框不知道如何下手,这次因为工作交接的缘故需要负责之前同事的Node后端部分,node,redis这些都是部署在Linux上的,看了几次运维的同学噼里啪啦的敲命令,黑的白的一闪一闪,我都还没看清楚敲的啥,敬佩之情油然而生。没办法,不能怂啊,于是开始学Linux,先装个虚......
  • 复习C语言-题目 1008: [编程入门]成绩评定
    题目描述给出一百分制成绩,要求输出成绩等级‘A’、‘B’、‘C’、‘D’、‘E’。90分以及90分以上为A,80-89分为B,70-79分为C,60-69分为D,60分以下为E。输入格式一个整数0-100以内输出格式一个字符,表示成绩等级样例输入复制90样例输出复制A1#inclu......
  • 复习C语言-题目 1009: [编程入门]数字的处理与判断
    题目描述给出一个不多于5位的整数,要求1、求出它是几位数2、分别输出每一位数字3、按逆序输出各位数字,例如原数为321,应输出123输入格式一个不大于5位的数字输出格式三行第一行位数第二行用空格分开的每个数字,注意最后一个数字后没有空格第三行按逆序输出......
  • WPF动画入门教程
    WPF动画入门教程 WindowsPresentationFoundation(WPF)是一种用于创建Windows客户端应用程序的UI框架。它让我们能够创建丰富的图形界面,包括各种各样的动画效果。接下来,我们将介绍如何在WPF中创建简单的动画。文章最后将给出源码,源码包括文章中的动画和一个水印按钮,一个简......
  • RocketMQ 入门实战(4)--Java 操作 RocketMQ
    本文主要介绍使用 Java 来操作RocketMQ,文中所使用到的软件版本:Java1.8.0_341、RocketMQ5.1.3、rocketmq-client-java5.0.5。1、引入依赖<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client-java</artifactId><versio......
  • OpenGL入门——着色器类
    着色器的编写、编译、管理是个很繁琐的事。所以就需要写一个类,这个类可以从文件读取着色器源码,可以编译链接它们,可以对它们进行错误检测,可以设置Uniform值。 1.类的声明#pragmaonce#include<glad/glad.h>#include<string>#include<fstream>#include<sstream>#inc......
  • 一文读懂ref函数:从入门到精通的具体操作指南
    什么是ref函数Vue3中的ref函数是一个用于创建响应式数据的函数。在Vue3中,当组件渲染时,会生成一个响应式数据对象,该对象包含了组件实例的所有数据属性。使用ref函数可以创建一个响应式的数据对象,并且可以在组件的生命周期内进行读取和修改。具体来说,ref函数接受一个初始值作为参数,并......
  • Reverse入门指北
     moectf{F1rst_St3p_1s_D0ne}Reverse入门指北两个附件,其中有一个exe,但是直接打开失败了  拖入die,无壳32位拖入ida,F5\shift+F12\ctrl+X都试了试,在shift+F12查看字符串中发现flag,简单解决 复制提交moectf{F1rst_St3p_1s_D0ne} ......