简体   繁体   English

Laravel 包开发在 serviceprovider 中注册刀片部分

[英]Laravel package development register blade section in serviceprovider

I'm building a package for the Laravel 5.6 framework.我正在为 Laravel 5.6 框架构建一个包。 I would like to register a blade @section('content');我想注册一个刀片@section('content'); so the developer can use @yield('content');所以开发者可以使用@yield('content'); and add that into it's own application.并将其添加到它自己的应用程序中。

Currently in my Package serviceProvider I register my views like this:目前在我的 Package serviceProvider 中,我注册了这样的视图:

public function boot()
{
    $this->loadViewsFrom(__DIR__.'/../resources/views', 'views');
}

However when add a @section('content');但是当添加一个@section('content'); to a view in that directory.到该目录中的视图。 I cannot use it ( @yield('content'); ) in my laravel application views.我不能在我的 Laravel 应用程序视图中使用它( @yield('content'); )。

I don't want to publish my package views to the Laravel application views!我不想将我的包视图发布到 Laravel 应用程序视图!

How could I achieve this?我怎么能做到这一点? I hope my question is well formulated.我希望我的问题表述得很好。 If you've any questions please let me know.如果您有任何问题,请告诉我。

Thanks!谢谢!

You cannot achieve this with yield() for this you have to extend blade.您无法使用yield()实现此目的,因此您必须扩展刀片。

Here is an example, In your ServiceProvider这是一个示例,在您的 ServiceProvider 中

 Blade::directive('datetime', function ($expression) {
        return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
    });

Now you can use it in your views.现在您可以在您的视图中使用它。

@datetime(Your date)

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM