laravel
目录
shell 命令
# 清除缓存命令组
# 开发时常用这些命令来确保修改生效
php artisan clear # 清除所有缓存
php artisan route:clear # 清除路由缓存
php artisan config:clear # 清除配置缓存
# 查看当前Laravel版本
# 或完整写法:php artisan --version
php artisan -V
# 创建API控制器(适用于RESTful API开发)
php artisan make:controller [ControllerName] --api
# 创建Eloquent模型(用于数据库交互)
# 可选参数:-m 同时创建迁移文件,-c 同时创建控制器,-r 创建资源控制器
php artisan make:model [ModelName]
# 创建表单请求类(用于请求验证)
# 生成的请求类位于app/Http/Requests目录,可用于验证规则定义
php artisan make:request [RequestName]
#安装 laravel-google-captcha
composer require mews/captcha
#发布配置文件
php artisan vendor:publish --provider="Mews\Captcha\CaptchaServiceProvider"
laravel练习
练习源码 :
链接: https://pan.baidu.com/s/1tbgeXfV1skeM_Y4vDoUObA?pwd=2gt9
提取码: 2gt9
Workerman + Laravel + Vue.js 聊天室
版本
OS windows
Laravel Framework 12.31.1
PHP 8.2.9
安装
1.安装laravel启动器
composer global require laravel/installer
安装成功还需要加入环境变量,本地路径示意:C:\Users\q1113\AppData\Roaming\Composer\vendor\bin
创建应用
laravel new example-app
npm install
npm run build
composer run dev
2.安装Node.js
没有npm命令
1.安装 NVM for Windows:
下载地址:https://github.com/coreybutler/nvm-windows/releases
下载 nvm-setup.exe 并安装
使用 NVM 安装 Node.js:
# 查看可用版本
nvm list available
# 安装 LTS 版本
nvm install lts
# 使用安装的版本
nvm use lts
# 验证
node --version
npm --version
3.安装GatewayClient
composer require workerman/gatewayclient
配置文件
composer.json
{
"$schema": "https://getcomposer.org/schema.json",
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": [
"laravel",
"framework"
],
"license": "MIT",
"require": {
"php": "^8.2",
"laravel/framework": "^12.0",
"laravel/tinker": "^2.10.1",
"laravel/ui": "^4.6",
"workerman/gatewayclient": "^3.1"
},
"require-dev": {
"fakerphp/faker": "^1.23",
"laravel/boost": "^1.8",
"laravel/pail": "^1.2.2",
"laravel/pint": "^1.24",
"laravel/sail": "^1.41",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.6",
"pestphp/pest": "^4.2",
"pestphp/pest-plugin-laravel": "^4.0"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"setup": [
"composer install",
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"@php artisan key:generate",
"@php artisan migrate --force",
"npm install",
"npm run build"
],
"dev": [
"Composer\\Config::disableProcessTimeout",
"npx concurrently -c \"#93c5fd,#c4b5fd,#fdba74\" \"php artisan serve\" \"php artisan queue:listen --tries=1\" \"npm run dev\" --names='server,queue,vite'"
],
"test": [
"@php artisan config:clear --ansi",
"@php artisan test"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force",
"@php artisan boost:update --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi",
"@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
"@php artisan migrate --graceful --ansi"
],
"pre-package-uninstall": [
"Illuminate\\Foundation\\ComposerScripts::prePackageUninstall"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
package.json
{
"$schema": "https://www.schemastore.org/package.json",
"private": true,
"type": "module",
"scripts": {
"build": "vite build",
"dev": "vite",
"watch": "vite build --watch"
},
"devDependencies": {
"@popperjs/core": "^2.11.6",
"@tailwindcss/vite": "^4.0.0",
"@vitejs/plugin-vue": "^6.0.1",
"axios": "^1.11.0",
"bootstrap": "^5.2.3",
"concurrently": "^9.0.1",
"laravel-vite-plugin": "^2.0.0",
"sass": "^1.56.1",
"tailwindcss": "^4.0.0",
"vite": "^7.0.7",
"vue": "^3.2.37"
},
"dependencies": {
"jquery": "^3.7.1"
}
}
vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/sass/app.scss',
'resources/js/app.js',
],
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
}),
],
resolve: {
alias: {
vue: 'vue/dist/vue.esm-bundler.js',
},
},
css: {
preprocessorOptions: {
scss: {
// 精确静默特定类型的弃用警告
silenceDeprecations: ['import', 'global-builtin', 'color-functions'],
// 同时启用 quietDeps
quietDeps: true,
},
},
},
});
laravel_worker\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\HomeController;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('/init', [HomeController::class, 'init']);
Route::post('/say', [HomeController::class, 'say']);
laravel_worker\resources\js\app.js
import './bootstrap';
import { createApp } from 'vue';
import ExampleComponent from './components/ExampleComponent.vue';
import ChatRoom from './components/ChatRoom.vue';
const app = createApp({});
app.component('example-component', ExampleComponent);
app.component('chat-room', ChatRoom);
app.mount('#app');
启动
双击项目 laravel_work\socket\GatewayWorker 目录下的 start_for_win.bat 脚本启动socket服务
启动内置的 PHP 开发服务器
php artisan serve
如果启动内置的 PHP 开发服务器php artisan serve无效,则在根目录下新建 start-server.php 文件,命令执行php start-server.php
<?php
// 自定义服务器启动脚本
$host = '127.0.0.1';
$port = 8080;
$publicDir = __DIR__ . '/public';
echo "启动 Laravel 开发服务器: http://{$host}:{$port}\n";
// 构建服务器命令
$command = sprintf(
'php -S %s:%d -t %s',
$host,
$port,
escapeshellarg($publicDir)
);
// 执行命令
passthru($command);
开发模式下,Vite 需要一个持续运行的开发服务器来提供资源,并保持终端运行.
npm run dev
监视文件变化自动编译
npm run watch
启动laravel服务器
php start-serve.php
登陆账号进入home目录 http://127.0.0.1:8080/home
请注意,php artisan make:auth 命令在 Laravel 6 及更高版本中已被移除。如果使用的是 Laravel 8 或更新版本,需要通过其他方式安装认证功能:
1.安装 Laravel UI 包:
composer require laravel/ui
2.生成脚手架:
# 生成基础的认证脚手架
php artisan ui bootstrap --auth
# 或者如果使用 Vue 等:
# php artisan ui vue --auth
3.编译前端资源:
npm install
composer run dev
laravel 13.x 文档
安装
安装 PHP 和 Laravel 安装程序
Windows 安装 PHP、Composer 和 Laravel 安装程序:
# Run as administrator...
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://php.new/install/windows/8.4'))
通过 Composer 安装 Laravel 安装程序:
composer global require laravel/installer
创建应用程序
创建新的 Laravel 应用程序:
laravel new example-app
使用开发 Composer 脚本启动 Laravel 的本地开发服务器、队列工作器和 Vite 开发服务器:
cd example-app
npm install && npm run build
composer run dev
若在windows运行命令 composer run dev 后,有如下等问题:
- php artisan pail 在 Windows 上因 pcntl 不可用失败
- php artisan serve 在当前 Windows 环境下对 127.0.0.1:8000 报 Failed to listen(可能与本机 TCP 绑定策略或环境限制有关)
修改脚本 composer.json 内容里的 scripts.dev 如下,再运行 composer run dev
{
"scripts": {
"dev": [
"Composer\\Config::disableProcessTimeout",
"npx concurrently -c \"#93c5fd,#c4b5fd,#fb7185,#fdba74\" \"php -S 0.0.0.0:8001 -t public\" \"php artisan queue:work --sleep=3 --tries=3\" \"npm run dev\" --names=server,queue,vite --kill-others"
],
}
安装 Laravel Boost
安装 Boost 作为开发依赖:
composer require laravel/boost --dev
运行交互安装程序:
php artisan boost:install
数据库与迁移
.env 配置文件的 DB_* 变量:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
选择使用非 SQLite 的数据库,你需要创建数据库并运行应用的数据库迁移 :
php artisan migrate
创建数据库可能报错:
-
Specified key was too long; max key length is 1000 bytes :
在 app/Providers/AppServiceProvider.php 中添加 Schema::defaultStringLength(191);。public function boot(): void { Schema::defaultStringLength(191); } -
Base table or view already exists:
php artisan migrate:fresh(删除数据库中的所有表,然后重新运行所有迁移)
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)