在 Laravel 5.5+ 應用中配置使用阿里云提供的短信服務及文件存儲服務(OSS)
  • 更新時間:2024-12-21 20:19:25
  • 分享
  • 發(fā)布時間:6年前
  • 6524

配置短信服務

基于Laravel框架的使用方法

  1. 安裝

composer?require?mrgoon/aliyun-sms?dev-master
  1. config/app.php中添加如下代碼

'providers'?=>?[

????//......此處省略大量代碼

????/**
?????*?阿里云短信
?????*/
????Mrgoon\AliSms\ServiceProvider::class,
],

//同時,可以選擇性添加aliases
'aliases'?=>?[

????//......此處省略大量代碼

????/**
?????*?阿里云短信
?????*/
????'AliSms'=>Mrgoon\AliSms\ServiceProvider::class,
],
  1. 添加系統(tǒng)服務

php?artisan?vendor:publish
#這里要選擇對應的編號
  1. 上一步會新增config/aliyunsms.php?文件

'access_key'????????=>?env('ALIYUN_ACCESSKEYID'),?//?accessKey
'access_secret'?????=>?env('ALIYUN_ACCESSKEYSECRET'),?//?accessSecret
'sign_name'?????????=>?env('ALIYUN_SMS_SIGN_NAME'),?//?簽名
  1. .env文件中添加環(huán)境變量:

ALIYUN_ACCESSKEYID=your?access?key
ALIYUN_ACCESSKEYSECRET=your?secret?key
ALIYUN_SMS_SIGN_NAME=sign?name
  1. 使用

$aliSms?=?new?AliSms();
$response?=?$aliSms->sendSms('phone?number',?'SMS_code',?['name'=>?'value?in?your?template']);
//dump($response);

非laravel框架的使用方法

  1. 加載方式通過composer,不變

  2. 使用樣例代碼如下:

$config?=?[
????'access_key'?=>?'your?access?key',
????'access_secret'?=>?'your?access?secret',
????'sign_name'?=>?'your?sign?name',
];

????$aliSms?=?new?Mrgoon\AliSms\AliSms();
????$response?=?$sms->sendSms(
????????'phone?number',?
????????'tempplate?code',?
????????['name'=>?'value?in?your?template'],?
????????$config
????);

配置圖片上傳OSS

  1. 安裝

composer?require?jacobcyl/ali-oss-storage
  1. config/app.php中添加如下代碼

'providers'?=>?[

????//......此處省略大量代碼

????/**
?????*?阿里云OSS
?????*/
????Jacobcyl\AliOSS\AliOssServiceProvider::class,
],

//同時,可以選擇性添加aliases
'aliases'?=>?[

????//......此處省略大量代碼

????/**
?????*?阿里云OSS
?????*/
????'AliOSS'=>Jacobcyl\AliOSS\AliOssServiceProvider::class,
],
  1. 編輯配置文件config/filesystems.php

'default'?=>?env('FILESYSTEM_DRIVER',?'local'),
.....

'disks'?=>?[

....
????????'oss'?=>?[
????????????'driver'?=>?'oss',
????????????'access_id'?=>?env('ALIYUN_ACCESSKEYID'),
????????????'access_key'?=>?env('ALIYUN_ACCESSKEYSECRET'),
????????????'bucket'?=>?env('ALIYUN_BUCKET'),
????????????'endpoint'?=>?env('ALIYUN_ENDPOINT'),

????????????//'endpoint_internal'?=>?'<internal?endpoint?[OSS內(nèi)網(wǎng)節(jié)點]?如:oss-cn-shenzhen-internal.aliyuncs.com>',

????????????//'cdnDomain'?=>?'<CDN?domain,?cdn域名>',

????????????'ssl'?=>?false,
????????????'isCName'?=>?false,
????????????'debug'?=>?false,
????????],

????],
  1. .env文件中添加環(huán)境變量:

ALIYUN_ACCESSKEYID=your?access?key
ALIYUN_ACCESSKEYSECRET=your?secret?key
ALIYUN_BUCKET=your?bucket
ALIYUN_ENDPOINT=your?endpoint

配置ueditor + OSS

  1. 安裝

composer?require?ilimpid/laravel-u-editor
  1. config/app.php中添加如下代碼

'providers'?=>?[

????//......此處省略大量代碼

????/**
?????*?百度編輯器
?????*/
????Stevenyangecho\UEditor\UEditorServiceProvider::class,
],

//同時,添加aliases
'aliases'?=>?[

????//......此處省略大量代碼

????/**
?????*?百度編輯器
?????*/
????'UEditor'=>Stevenyangecho\UEditor\UEditorServiceProvider::class,
],
  1. 添加系統(tǒng)服務

php?artisan?vendor:publish
  1. 上一步會新增config/UEditorUpload.php?文件

'mode'?=>?'aliyun',//上傳方式,local?為本地???qiniu?為七牛??aliyun為阿里云

'aliyun'?=>?[
????'accessKeyId'?=>?env('ALIYUN_ACCESSKEYID'),
????'accessKeySecret'?=>?env('ALIYUN_ACCESSKEYSECRET'),
????'endpoint'?=>?env('ALIYUN_ENDPOINT'),
????'url'?=>?'http://'.env('ALIYUN_BUCKET').'.'.env('ALIYUN_ENDPOINT'),
????'bucket'?=>?env('ALIYUN_BUCKET'),
????'directory'?=>?'images/'.date('Ymd',time()),
],

//注意:編輯器上傳圖片的時候會在本地保存一張,下面配置必須設置,images必須保持一致

/*?上傳圖片配置項?*/
'upload'?=>?[
????/*?上傳保存路徑,可以自定義保存路徑和文件名格式?*/
????"imagePathFormat"?=>?"/storage/images/{yyyy}{mm}{dd}/{time}{rand:6}",?
],
  1. .env文件中添加環(huán)境變量:

ALIYUN_ACCESSKEYID=your?access?key
ALIYUN_ACCESSKEYSECRET=your?secret?key
ALIYUN_BUCKET=your?Bucket?key
ALIYUN_ENDPOINT=your?Endpoint?key

非laravel-admin使用

@include(UEditor::head)

<div?class="col-sm-10">
????<script?id="ueditor"></script>
????<script>
????????var?ue?=?UE.getEditor("ueditor");
????????ue.ready(function?()?{
????????????//由于Laravel有防csrf防偽造攻擊的解決所以加上此行
????????????ue.execCommand(serverparam,?_token,?{{?csrf_token()?}});
????????});
????</script>
</div>

基于 laravel-admin 使用

  1. 增加組件文件app/Admin/Extensions/Form/uEditor.php

<?php

namespace?App\Admin\Extensions\Form;
use?Encore\Admin\Form\Field;

class?uEditor?extends?Field
{
????//?定義視圖
????protected?$view?=?'admin.uEditor';

????//?css資源
????protected?static?$css?=?[];

????//?js資源
????protected?static?$js?=?[
????????'laravel-u-editor/ueditor.config.js',
????????'laravel-u-editor/ueditor.all.min.js',
????????'laravel-u-editor/lang/zh-cn/zh-cn.js'
????];

????public?function?render()
????{
????????$this->script?=?<<<EOT
????????//解決第二次進入加載不出來的問題
????????UE.delEditor("ueditor");
????????//?默認id是ueditor
????????var?ue?=?UE.getEditor('ueditor',?{
????????????//?自定義工具欄
????????????toolbars:?[
????????????????['bold',?'italic',?'underline',?'strikethrough',?'blockquote',?'insertunorderedlist',?'insertorderedlist',?'justifyleft',?'justifycenter',?'justifyright',?'link',?'insertimage',?'source',?'fullscreen']
????????????],
????????????elementPathEnabled:?false,
????????????enableContextMenu:?false,
????????????autoClearEmptyNode:?true,
????????????wordCount:?false,
????????????imagePopup:?false,
????????????autotypeset:?{indent:?true,?imageBlockLine:?'center'}
????????});?
????????ue.ready(function?()?{
????????????ue.execCommand('serverparam',?'_token',?'{{?csrf_token()?}}');
????????});

EOT;
????????return?parent::render();
????}
}
  1. 增加視圖文件resources/views/admin/uEditor.blade.php

<div?class="form-group?{!!?!$errors->has($errorKey)??:?'has-error'?!!}">
????<label?for="{{$id}}"?class="col-sm-2?control-label">{{$label}}</label>
<div?class="col-sm-8">
????@include('admin::form.error')
????{{--?這個style可以限制他的高度,不會隨著內(nèi)容變長?--}}
????<textarea?type='text/plain'?style="height:400px;"?id='ueditor'?id="{{$id}}"?name="{{$name}}"?placeholder="{{?$placeholder?}}"?{!!?$attributes?!!}??class='ueditor'>
????????????{!!?old($column,?$value)?!!}
????????</textarea>
????@include('admin::form.help-block')
</div>
</div>
{{--?注意:如果你實用script標簽有一些奇怪的問題,更換textarea就可以解決了。?--}}
  1. 然后注冊進laravel-admin,在app/Admin/bootstrap.php中添加以下代碼:

<?php

/**
?*
?*/

use?App\Admin\Extensions\Form\uEditor;
use?Encore\Admin\Form;

Form::extend('ueditor',?uEditor::class);

//Encore\Admin\Form::forget(['map',?'editor']);
  1. 再控制器的form()里面調(diào)用

$form->ueditor('content',?'內(nèi)容')->rules('required');


我們專注高端建站,小程序開發(fā)、軟件系統(tǒng)定制開發(fā)、BUG修復、物聯(lián)網(wǎng)開發(fā)、各類API接口對接開發(fā)等。十余年開發(fā)經(jīng)驗,每一個項目承諾做到滿意為止,多一次對比,一定讓您多一份收獲!

本文章出于推來客官網(wǎng),轉(zhuǎn)載請表明原文地址:https://www.tlkjt.com/marketing/76.html
推薦文章

在線客服

掃碼聯(lián)系客服

3985758

回到頂部