cell-blog 开发记录

2022-02-25 17:34:49 浏览数 (1)

Cell Blog 开发记录,项目地址

搭建 php 环境

  1. 安装 wampserver
  2. 安装 composer
  3. 更换 aliyun 源

1

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

新建 laravel 项目

1

composer create-project --prefer-dist laravel/laravel cell-blog "7.*"

或者

1 2

composer global require laravel/installer laravel new blog

下载 debugbar

1

composer require barryvdh/laravel-debugbar --dev

修改语言时区

修改 config/app.php,将 local 的值 en 改成 zh-CN(laravel-admin 自带 zh-CN):

1 2 3 4

# 时区 'timezone' => 'Asia/Shanghai', # 语言 'locale' => 'zh-CN',

配置数据库

首先确保安装好了 laravel,并且数据库连接设置正确。

1 2 3 4 5 6 7 8

APP_URL=http://cell.blog DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3307 DB_DATABASE=cell_blog DB_USERNAME=root DB_PASSWORD=123456

下载 laravel-admin

1 2

cd cell-blog composer require encore/laravel-admin

卸载命令composer remove xxx

然后运行下面的命令来发布资源:

1

php artisan vendor:publish --provider="EncoreAdminAdminServiceProvider"

在该命令会生成配置文件config/admin.php,可以在里面修改安装的地址、数据库连接、以及表名,建议都是用默认配置不修改。

然后运行下面的命令完成安装:

1

php artisan admin:install

运行这个命令的时候,如果遇到了下面的错误: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter tableusersadd uniqueusers_email_unique(email)) 参考这个 issue 来解决 https://github.com/z-song/laravel-admin/issues/1541 在appProvidersAppServiceProvider.php添加默认值

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

<?php namespace AppProviders; use IlluminateSupportServiceProvider; use IlluminateSupportFacadesSchema; //add fixed sql class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); //add fixed sql } /** * Register any application services. * * @return void */ public function register() { // } }

启动服务后,在浏览器打开 http://localhost/admin/ , 使用用户名 admin 和密码 admin 登录。

报错Disk [admin] not configured, please add a disk config in config/filesystems.php

config/filesystems.php中 disks 处添加以下配置后执行php artisan storage:link来创建软链接(windows 和 linux 的软链接不一样不能直接复制!)

宝塔执行时删除禁用函数 putenv(),symlink()

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

'admin' => [ 'driver' => 'local', 'root' => storage_path('app/public/system'), 'url' => env('APP_URL').'/storage/system', 'visibility' => 'public', ], 或 'admin' => [ 'driver' => 'local', 'root' => public_path('uploads'), 'url' => env('APP_URL').'/public/uploads/', 'visibility' => 'public', ],

下载 dcat-admin

1

composer require dcat/laravel-admin

然后运行下面的命令来发布资源:

1

php artisan admin:publish

在该命令会生成配置文件config/admin.php,可以在里面修改安装的地址、数据库连接、以及表名,建议都是用默认配置不修改。

然后运行下面的命令完成安装:

1

php artisan admin:install

迁移文件创建表

1 2 3 4 5 6 7 8 9

php artisan make:migration create_articles_table php artisan make:migration create_tags_table php artisan make:migration create_categories_table php artisan make:migration create_article_tags_table php artisan make:migration create_navigations_table php artisan make:migration create_friendship_links_table php artisan make:migration create_pages_table php artisan make:migration create_systems_table php artisan make:migration create_mottoes_table

运行迁移

1

php artisan migrate

创建模型 model

1 2 3 4 5 6 7 8

php artisan make:model Models/Article php artisan make:model Models/Category php artisan make:model Models/Tag php artisan make:model Models/Navigation php artisan make:model Models/FriendshipLink php artisan make:model Models/Page php artisan make:model Models/System php artisan make:model Models/Motto

创建控制器

1 2 3 4 5 6 7 8

php artisan admin:make ArticleController --model=AppModelsArticle php artisan admin:make CategoryController --model=AppModelsCategory php artisan admin:make TagController --model=AppModelsTag php artisan admin:make NavigationController --model=AppModelsNavigation php artisan admin:make FriendshipLinkController --model=AppModelsFriendshipLink php artisan admin:make PageController --model=AppModelsPage php artisan admin:make SystemController --model=AppModelsSystem php artisan admin:make MottoController --model=AppModelsMotto

添加后台路由

app/Admin/routes.php

1 2 3 4 5 6 7 8

$router->resource('articles', ArticleController::class); $router->resource('categories', CategoryController::class); $router->resource('tags', TagController::class); $router->resource('navigations', NavigationController::class); $router->resource('friendship-links', FriendshipLinkController::class); $router->resource('pages', PageController::class); $router->resource('systems', SystemController::class); $router->resource('mottoes', MottoController::class);

editormd 安装

editormd github 图像问题

存放本地表情

1

publicvendorlaravel-admin-exteditormdeditormd-1.5.0imagesemojis

修改 editormd.js 及 editormd.min.js

1 2 3 4 5

// Emoji graphics files url path editormd.emoji = { path : "/iamges/emojis/", ext : ".png" };

图片上传

csrf 419 错误

可以在VerifyCsrfToken.php中添加白名单跳过验证,或者手动添加 csrf 验证器:

修改 image-dialog.js 的var dialogContent 参考

1 2 3 4 5 6 7 8 9 10

if (settings.crossDomainUpload) { action = "&callback=" settings.uploadCallbackURL "&dialog_id=editormd-image-dialog-" guid; } //添加 csrf 验证 var csrfToken = $('meta[name="csrf-token"]').attr('content'); var csrfField = ""; if (csrfToken) { csrfField = "<input type='hidden' name='_token' value='" csrfToken "' />"; }

markdown 转 HTML

https://www.zhiqiexing.com/119.html Laravel Markdown 安装

1 2

composer require graham-campbell/markdown php artisan vendor:publish

扩展表格

1

composer require league/commonmark

config/markdown.php

1 2 3

'extensions' => [ LeagueCommonMarkExtensionTaskListTaskListExtension::class, ],

Eloquent 模型事件

Eloquent 模型可以触发事件,允许你在模型生命周期中的多个时间点调用如下这些方法:retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored。事件允许你在一个指定模型类每次保存或更新的时候执行代码。

retrieved 事件会在从数据库中获取已存在模型时触发。当一个新模型被首次保存的时候,creating 和 created 事件会被触发。如果一个模型已经在数据库中存在并调用 save 方法,updating/updated 事件会被触发,无论是创建还是更新,saving/saved 事件都会被触发。

添加后台验证码

依赖 php 扩展fileinfo 添加验证码

Media manager

1 2

composer require laravel-admin-ext/media-manager php artisan admin:import media-manager

‘extensions’ => [ ‘media-manager’ => [ // Select a local disk that you configured in config/filesystem.php ‘disk’ => ‘public’ ], ],

1 2

# 给 laravel-admin 增加锁屏功能

composer require laravel-admin-ext/lock-screen

1 2 3 4 5 6 7 8

'route' => [ 'prefix' => 'demo', 'namespace' => 'App\Admin\Controllers', // 在中间件数组中加上'admin.lock' 'middleware' => ['web', 'admin', 'admin.lock'], ],

前台设计

创建控制器

1 2 3 4 5

php artisan make:controller HomeController php artisan make:controller ArticleController php artisan make:controller CategoryController php artisan make:controller TagController php artisan make:controller PageController

后台 img 灯箱

https://github.com/laravel-admin-extensions/grid-lightbox

1 2 3

composer require laravel-admin-ext/grid-lightbox php artisan vendor:publish --tag=laravel-admin-grid-lightbox

0 人点赞