关于php:Laravel-markdown渲染输出到blade模版

原文地址:https://www.wjcms.net/archive…

前言

昨天,公布了laravel反对markdown编辑器的文章,还附上了配置图片上传,然而有网友问怎么在blade模版中渲染输入,这里写个文章记录一下。

装置扩大包

Laravel Markdown须要PHP 7.2-8.0 。此特定版本反对Laravel 6-8。

对照上边的表,抉择对应适合的版本,这里我的版本是8,所以装置13.1版本。

composer require graham-campbell/markdown:^13.1

在我装置的时候发现报错:

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///www/server/php/74/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223
 
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///www/server/php/74/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223
 
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.#   

所以这里咱们应用如下命令进行装置:

php -d memory_limit=-1 /usr/bin/composer require graham-campbell/markdown:^13.1

上述命令中的/usr/bin/composer,为composer装置地址
可应用
composer -h
命令进行获取。

配置providers

//cconfig/app.php
'providers' => [
    //增加如下一行
    GrahamCampbell\Markdown\MarkdownServiceProvider::class,
]

配置alias

 'Markdown' => GrahamCampbell\Markdown\Facades\Markdown::class,

拷贝相干文件到我的项目文件夹中

php artisan vendor:publish --provider="GrahamCampbell\Markdown\MarkdownServiceProvider"

控制器中应用

  1. 简略应用
use GrahamCampbell\Markdown\Facades\Markdown;

Markdown::convertToHtml('foo'); // <p>foo</p>
  1. 依赖注入的写法
use Illuminate\Support\Facades\App;
use League\CommonMark\MarkdownConverterInterface;

class Foo
{
    protected $converter;

    public function __construct(MarkdownConverterInterface $converter)
    {
        $this->converter = $converter;
    }

    public function bar()
    {
        return $this->converter->convertToHtml('foo');
    }
}

App::make('Foo')->bar();

blade模版中应用

@markdown
{{$data->content}}
@endmarkdown

更多内容参考官网文档。
https://github.com/GrahamCamp…

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理