cell-blog 开发记录

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="Encore\Admin\AdminServiceProvider"

在该命令会生成配置文件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
app\Providers\AppServiceProvider.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 App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; //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=App\Models\Article
php artisan admin:make CategoryController --model=App\Models\Category
php artisan admin:make TagController --model=App\Models\Tag
php artisan admin:make NavigationController --model=App\Models\Navigation
php artisan admin:make FriendshipLinkController --model=App\Models\FriendshipLink
php artisan admin:make PageController --model=App\Models\Page
php artisan admin:make SystemController --model=App\Models\System
php artisan admin:make MottoController --model=App\Models\Motto

添加后台路由

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
public\vendor\laravel-admin-ext\editormd\editormd-1.5.0\images\emojis

修改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' => [
League\CommonMark\Extension\TaskList\TaskListExtension::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
1
2
3
4
5
6
'extensions' => [
'media-manager' => [
// Select a local disk that you configured in `config/filesystem.php`
'disk' => 'public'
],
],

给laravel-admin增加锁屏功能

1
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%