Home  >  Article  >  PHP Framework  >  How to quickly integrate Emoji expression packs in Laravel project practice

How to quickly integrate Emoji expression packs in Laravel project practice

Mini
MiniOriginal
2020-05-20 20:13:10116browse

随着互联网的快速发展,光打字就没什么意思了,还是需要图片、Emoji 表情包充当更幽默的角色,而现在的各大网站、APP 上发文章,聊天,甚至视频上的动画都需要表情进行丰富,深感 Emoji 表情包无处不在,无论是 QQ、微信还是各大论坛,到处都是表情包的身影,现在我们也是要 Laravel 框架集成表情包进入功能开发。随着互联网的快速发展,光打字就没什么意思了,还是需要图片、Emoji 表情包充当更幽默的角色,而现在的各大网站、APP 上发文章,聊天,甚至视频上的动画都需要表情进行丰富,深感 Emoji 表情包无处不在,无论是 QQ、微信还是各大论坛,到处都是表情包的身影,现在我们也是要 Laravel 框架集成表情包进入功能开发。

PHP 7 对 Unicode 字符串提供了更好的支持,我们可以更方便地显示表情,我们需要使用可读性更好的方式来实现表情显示,在 Laravel 中我们可以通过 Laravel Emoji 这个扩展包来实现这个功能。PHP 7 对 Unicode 字符串提供了更好的支持,我们可以更方便地显示表情,我们需要使用可读性更好的方式来实现表情显示,在 Laravel 中我们可以通过 Laravel Emoji 这个扩展包来实现这个功能。

表情太丰富了实例展示:

How to quickly integrate Emoji expression packs in Laravel project practice

一、Laravel 的安装,前面我们进行 Laravel 的安装(请参考 laravel 手册或者移步到前面章节)

实例代码:

composer create-project --prefer-dist laravel/laravel blog
php artisan key:generate

二、 composer.json 配置组件包

在 require 中添加 "unicodeveloper/laravel-emoji": "1.0.*

“require”:{
    “php”: “^7.1.3”,
    “fideloper/proxy”: “^4.0”,
    “laravel/framework”: “5.8.*”,
    “laravel/tinker”: “^1.0”,
    “jacobcyl/ali-oss-storage”: “^2.1”,
    “unicodeveloper/laravel-emoji":"1.0.*”
},

然后执行命令:

composer update

执行完成后,项目中就出现内置的部分表情包了以及表情包类文件,如下:

三、提供注册服务

安装完成之后,需要注册服务提供者,在配置文件 app.php 中,添加如下这段代码到 providers 数组的最后,同时注册门面到 aliases 数组:

<?php
&#39;providers&#39;=>[
    ... 
   Unicodeveloper\Emoji\EmojiServiceProvider::class,],
&#39;aliases&#39;=>[ 
    ...
    &#39;Emoji&#39;=>Unicodeveloper\Emoji\Facades\Emoji::class,
]

四、设置路由

我们还是写在 index 方法里面,方便测试。

Route::get(&#39;/index/index&#39;,&#39;\App\Http\Controllers\Index\IndexController@index&#39;);

五、编程程序实现表情包转换。

<?php
namespace App\Http\Controllers\Index;
use App\Http\Controllers\Controller;
use Unicodeveloper\Emoji\Emoji;
class IndexController extends Controller {
    public function index(){
       $em = new Emoji();
       //根据别名转换        
       $res = $em->findByAlias("laughing");
       //根据名称转换        
       $res1 = $em->findByName("grinning");
       //根据编码格式转换成名称        
       $res2 = $em->findByUnicode("\\u{1F617}");
       print_r($res);
       print_r($res1);
       print_r($res2);
       exit;
   //return [&#39;url&#39;=>&#39;test&#39;];    
    }
}

六、访问结果(自己设置的路由)

How to quickly integrate Emoji expression packs in Laravel project practice

七、文档说明

如果感觉里面的表情包太少,那么我们可以去官网:

http://unicode.org/emoji/charts/full-emoji-list.html

我就部分截图如下,需要更多表情自行下载安装,丰富你的项目内容:

How to quickly integrate Emoji expression packs in Laravel project practice

(这些表情包,我都心动了,太好看了)

感兴趣的同学请移步到公众号(Laravel 技术社区)。

The above is the detailed content of How to quickly integrate Emoji expression packs in Laravel project practice. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn