简体   繁体   English

在自定义cms中创建类似于WordPress的小部件

[英]Creating widget like WordPress in custom cms

我想在自定义cms上在WordPress中创建最新的帖子小部件等,从哪里开始的任何建议都很棒

Widgets in WordPress are basically objects that can be "hooked" into particular places in a theme. WordPress中的小部件基本上是可以“钩”到主题中特定位置的对象。 The most similar mechanism in Laravel would be Events , I'd say. 我想说,Laravel中最类似的机制是事件

Wherever you want a widget to appear in your CMS, you would fire an event, say "WidgetsGoHere", and insert the response(s) from that event into your view. 无论您希望在CMS中显示小部件的何处,都将触发一个事件,说“ WidgetsGoHere”,然后将事件的响应插入视图中。

Then, I'd probably create an Event Subscriber class for each widget (and probably a base class of Widget from which all widgets could inherit helpful code.) The event subscriber would listen for the WidgetsGoHere event, then build whatever it needed to output—a bunch of HTML, presumably—and return it as a response. 然后,我可能会为每个小部件(可能是小部件的基类,所有小部件都可以从中继承有用的代码)创建一个事件订阅者类。事件订阅者将侦听WidgetsGoHere事件,然后构建需要输出的内容—大概是一堆HTML,并将其作为响应返回。 The code that fired the event would then output the response. 然后,触发事件的代码将输出响应。

In WordPress, a theme or plugin registers any widgets it uses as part of the theme or plugin startup code. 在WordPress中,主题或插件会注册它用作主题或插件启动代码一部分的所有小部件。 If you haven't developed plugins or themes for your CMS yet, you may want to add some code (perhaps in "start") to create your widgets and get them listening for events. 如果尚未为CMS开发插件或主题,则可能需要添加一些代码(也许在“开始”中)以创建窗口小部件并使它们侦听事件。 You can hardcode them to start with, and perhaps move to a more dynamic system once you've got things working. 您可以从一开始就对它们进行硬编码,并且一旦工作正常,也许可以转移到一个更具动态性的系统。

Here's a very simple worked example. 这是一个非常简单的示例。 We'll create three "widgets". 我们将创建三个“小部件”。 Start with a standard install of Laravel. 从标准安装Laravel开始。 Then, edit app\\Providers\\EventServiceProvider.php and change the $listen variable there as follows: 然后,编辑app\\Providers\\EventServiceProvider.php并在其中更改$listen变量,如下所示:

protected $listen = [
    'App\Events\WantWidgets' => [
        'App\Handlers\WidgetOne',
        'App\Handlers\WidgetTwo',
        'App\Handlers\WidgetThree',
    ],
];

Save that and then run the artisan command php artisan event:generate . 保存该内容,然后运行artisan命令php artisan event:generate This will read the configuration we've just provided and create an event called WantWidgets and three different event listeners, which will be our three widgets. 这将读取我们刚刚提供的配置,并创建一个名为WantWidgets的事件和三个不同的事件侦听器,这将是我们的三个小部件。 You should be able to see the new classes in app\\Events and app\\Handlers . 您应该能够在app\\Eventsapp\\Handlers看到新类。

Then, change WelcomeController.php so that the WelcomeController index page fires the event and passes the responses it gets back to its view: 然后,更改WelcomeController.php以便WelcomeController索引页面触发该事件并将其返回的响应传递回其视图:

public function index()
{
    $widgets = Event::fire(new \App\Events\WantWidgets);
    return view('welcome')->with('widgets', $widgets);
}

In the view ( resources\\views\\welcome.blade.php ) take the "widgets" you're now passed and output them: 在视图( resources\\views\\welcome.blade.php )中,获取您现在通过的“窗口小部件”并将其输出:

<ul>
    @foreach ($widgets as $widget)
        <li>{{ $widget}}</li>
    @endforeach
</ul>

If you fire up the web app now, you'll find that you don't get very much interesting stuff out, because the "widgets" are currently outputting nothing. 如果您现在启动Web应用程序,您会发现您没有得到很多有趣的东西,因为“小部件”当前什么也不输出。 You should at least see three list item dots. 您至少应该看到三个列表项点。

Now, change the "widgets" to return something. 现在,更改“小部件”以返回某些内容。 For example, edit app\\Handlers\\WidgetOne.php so that its handle method returns a simple string: 例如,编辑app\\Handlers\\WidgetOne.php ,使其handle方法返回一个简单的字符串:

public function handle(WantWidgets $event)
{
    return "I could be a widget when I grow up";
}

And when you refresh the page, you should see that string as one of the list items. 并且当刷新页面时,您应该看到该字符串作为列表项之一。

This is similar to the way the WordPress widget system works—at the point in the page you want widgets to appear, you fire an event. 这类似于WordPress窗口小部件系统的工作方式-在您希望窗口小部件出现的页面中,您触发一个事件。 Widgets are "registered" to that event and "draw" themselves, outputting HTML to the page at that point. 窗口小部件被“注册”到该事件并“绘制”自身,此时将HTML输出到页面。

That should get you started. 那应该让您开始。 Virtually everything about this can be improved. 几乎所有与此有关的方面都可以得到改善。 For example, your widgets might use their own blade template to render their HTML into a string and return that. 例如,您的小部件可以使用自己的刀片模板将HTML渲染为字符串并返回。

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

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